用Jbuilder8做一个简单的struts示例

类别:Java 点击:0 评论:0 推荐:

用Jbuilder8做一个简单的struts示例

最近在看Wiley出版的《Mastering Jakarta Struts》(PDF格式)时发现很多例子包括配置文档等都是直接用手工写的,工作量比较大。为此考虑使用IDE来简化工作。JBUILDER8是个非常流行的java工具并且可以和多种WEB SERVER集成工作。本文中的例子在tomcat4.x和weblogic7.1均能正常工作。(本文不讨论JB8与weblogic的集成配置,如果有需要JB8与weblogic集成的配置文件可以发mail索取,我的mail是:[email protected])。本文中的例子是Mastering Jakarta Struts》第三章的例子。

下面开始进入主题:

1.            首先建立一个工程(project)(File->New Project)

取一个名字(本文使用myfirststruts),你也可以起一个自己中意的名字(如图)并点击“finish”按钮完成。

设置工程要采用的web server(Project->Project Properties->Server)见图

即可以选择tomcat4.x也可以选择weblogic7.1,要更具具体情况,我选择了tomcat4.0;

2.            创建JSP文件(即VIEW)

File->New->Web->Java Server Page,在Name框中输入index其它的均不选取,点击“finish”创建完成;见图

以同意的方法创建一个名称为quote.jsp文件。两个文件的源代码分别为:

index.jsp

<html>

<head>

<title>Wiley Struts Application</title>

</head>

<body>

<table width="500"

border="0" cellspacing="0" cellpadding="0">

<tr>

<td>&nbsp;</td>

</tr>

<tr bgcolor="#36566E">

<td height="68" width="48%">

<div align="left">

<img src="images/hp_logo_wiley.gif" width="220" height="74">

</div>

</td>

</tr>

<tr>

<td>&nbsp;</td>

</tr>

</table>

<form action="Lookup" name="lookupForm" type="myfirststruts.LookupForm" >

<table width="45%" border="0">

<tr>

<td>Symbol:</td>

<td><input type=text name="symbol" ></td>

</tr>

<tr>

<td colspan="2" align="center"><input type=”submit” name=”submit1”></td>

</tr>

</table>

</form>

</body>

</html>

quote.jsp

<%@ page contentType="text/html; charset=GBK" %>

<html>

<head>

<title>Wiley Struts Application</title>

</head>

<body>

<table width="500"

border="0" cellspacing="0" cellpadding="0">

<tr>

<td>&nbsp;</td>

</tr>

<tr bgcolor="#36566E">

<td height="68" width="48%">

<div align="left">

<img src="images/hp_logo_wiley.gif"

width="220" height="74">

</div>

</td>

</tr>

<tr>

<td>&nbsp;</td>

</tr>

<tr>

<td>&nbsp;</td>

</tr>

<tr>

<td>&nbsp;</td>

</tr>

<tr>

<td>

Current Price : <%= request.getAttribute("PRICE") %>

</td>

</tr>

<tr>

<td>&nbsp;</td>

</tr>

</table>

</body>

</html>

3.          将index.jsp转换成STRUTS

右击窗口左边的工程浏览树中的index.jsp文件,选择convert to struts选项(见图)

如果JSP and HTML files to convert to Struts下面的框中没有index.jsp,请用右边的“Add Files…”按钮来选取index.jsp文件;

单击Finish按钮完成次步;你可以看到原来的JSP代码有所改变,请不要修改,继续往下走。

4.            创建与index.jsp对应的ActionForm,它非常类似于一个JAVABEAN,只不过它的getter和setter要与对于的页面(本例为index.jsp)中FROM中包含的表单参数。由于index.jsp中只有一个名为symbol的表单参数因此这个ActionForm的主要部分将是:getSymbol()和setSymbol();用File->New->Web->ActionForm来生成一个ActionForm,在下面的页面中的actionForm框中输入要创建的ActionForm名称,此处为LookupForm,其它不要改变;

点击Next进入下一步,并在页面上单击Add From JSP按钮,重列表中选择index.jsp

单击Next,然后再单击Finish完成。不要改动生成的LookupForm.java代码;

5.            创建Controller控件,Struts的Controller部分基本包含两部分,一个是ActionServlert,另一个是Action。本例中ActionServlet采用Struts自带的类。下面我们自己创建一个Action类;用File->New->Web->Action来实现。第一步出现如下图,在Action框中填写要创建的类的名称。本例为LookupAction,其它步改动;

单击Next进入下一个页面,设置如下:

单击Finish完成;并将生成的类文件LookupAction.java源代码改为

package myfirststruts;

 

import org.apache.struts.action.*;

import javax.servlet.http.*;

 

public class LookupAction extends Action

{

  protected Double getQuote(String symbol)

  {

    if (symbol.equalsIgnoreCase("SUNW"))

    {

      return new Double(25.00);

    }

    return null;

  }

 

  public ActionForward perform(ActionMapping mapping, ActionForm form,

                            HttpServletRequest request, HttpServletResponse response)

  {

    Double price = null;

    // Default target to success

    String target = new String("success");

    if (form != null)

    {

      // Use the LookupForm to get the request parameters

      LookupForm lookupForm = (LookupForm) form;

      String symbol = lookupForm.getSymbol();

      price = getQuote(symbol);

    }

    if (price == null)

    {

      target = new String("failure");

    }

    else

    {

      request.setAttribute("PRICE", price);

    }

    // Forward to the appropriate View

    return (mapping.findForward(target));

  }

}

6.            修改配置文件

将左边的工程目录结构树中的DefaultWebApp项展开,双击struts-config.xml,出现下面的图

选择其中的Action Mappings,出现下图

选择Path框中的/lookupAction,并单击右边的Edit按钮。出现如下图

选择下面的Forwards分组页,并单击两次Add按钮,会在Forwards中添加两项,分别双击进行修改(或选中后用右边的Edit可以进行修改),修改的结果是:

添加的信息分别是

index.jsp                                    failure

quote.jsp                                          success

7.            运行

右击index.jsp,从弹出的菜单中选择Web Run Uing “index”,待页面出现后(如果有错误重复检查以前的步骤),拷贝其URL,打开IE并输入拷贝的URL

将出现

当我们输入sunw时会出现,输入其它的任何值,仍然会导向到上个页面

此示例可在不作任何修改的情况下可以运行在weblogic7.1下。已经调试通过。

本文地址:http://com.8s8s.com/it/it17355.htm