创建你的第一个Struts应用
你已经完成了struts包的下载和安装,现在可以开发Struts应用了。我们的应用由查询股票号码的简单的JSP页面来完成,它将返回被查询股票的价格。我们会通过这个例子来说明在创建Struts应用中必须执行哪些步骤。因为Struts技术来源于MVC设计模式,所以在所有基于Struts的开发中,你可以遵循MVC模式这种标准来进行处理。处理方式为:从视图开始,控制器对象操纵模型组件来为视图服务。这个处理过程通过下面步骤来实现:
1.定义并创建实现某一功能的相关视图,它们是应用中的用户接口。在struts-config.xml这个struts配置文件中所有的ActionForm 就是我们要创建的视图。
2.创建控制器组件。
3.在struts-config.xm文件中定义视图,控制器之间的关系。
4.在web.xml文件中配置启动struts的一些必要信息。
5.启动应用。
上述步骤只是对Struts开发做了概要描述,在接下来的章节我们将会对这些步骤进行比较细致的描述。
创建视图
在创建struts应用的视图时,我们会先创建一个包含Struts标签的JSP文件。
当前有三个主要的struts标签,Bean, HTML和Logic。在第5章我们会集中关注在这几个标签库上,但这章我们在视图中主要关注于HTML标签库。在开始我们的应用时,我们首先要需要描述视图接口。在下面的应用中有两个视图:index.jsp和quote.jsp。
 
Index视图
Index视图有index.jsp文件来表现,是我们第一个看到的视图。它用来查询股票和提交
请求到相应的Action中。Index.jsp源文件如下:
------------------------------------------------------------
Listing 3.1: index.jsp.
<%@ page language="java" %>
<%@ taglib
uri="/WEB-INF/struts-html.tld"
prefix="html" %>
<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>
<html:form action="Lookup"
name="lookupForm"
type="wiley.LookupForm" >
<table width="45%" border="0">
<tr>
<td>Symbol:</td>
<td><html:text property="symbol" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit /></td>
</tr>
</table>
</html:form>
</body>
</html>
 
如你看到的index视图,除了form标签外,它就象一个包含数据集合的HTML页面。在HTML页面中我们把FORM标签改为用Struts标签<html:form />来替换。
 
 三章:开始学习Struts(2)ActionForm 
ActionForm用来保存视图中表单输入参数的实际数值。如我们在前面章节提到的,当一个<html:form />标签被提交,Struts框架把对应的数据组装到ActionForm实例中。Struts框架通过JavaBean影射,因此ActionForm中的成员变量必须遵守JavaBean的命名规则。举例如下:
private String symbol;
public void setSymbol(String symbol);
public String getSymbol();
在这个例子中有一个简单成员变量symbol。在获取或设置成员变量的数值时必须带有前缀set和get,在set和get后面的成员变量的第一个字符必须为大写。列表3-2是ActionForm的源代码
LookupForm.java.
--------------------------------------------------------------------------------
package wiley;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LookupForm extends ActionForm {
private String symbol = null;
public String getSymbol() {
return (symbol);
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public void reset(ActionMapping mapping,
HttpServletRequest request) {
this.symbol = null;
}
}
---------------------------------------------------------
在这个类中没有特殊的东西,它只是简单的继承org.apache.struts.action.ActionForm,以及每个ActionForm实例对每个成员变量必须实现的get,set方法。在这个ActionForm  bean中有一个特殊的reset()方法。每一个使用LookupForm的请求都会执行reset()方法。这个方法的目的是为了重置LookupForm的成员变量,以便下一个应用使用。
注意:reset()方法ActionMapping类来被引用。关于ActionMapping类我们暂时可以忽略,将在第4,5章中有详细的描述。
编译LookupForm类,移到<CATALINA_HOME>/webapps/wileystruts/WEB-INF/classes/wiley目录下,在<CATALINA_HOME>/webapps/wileystruts/WEB-INF/struts-config.xml文件中添加<form-bean name="lookupForm" type="wiley.LookupForm"/>
这是为了让Struts应用知道LookupForm如何被引用。

解决方案 »

  1.   

    Quote 视图
    我们最后的视图是quote.jsp。视图在用户成功的寻找到股票后显示。它是没有特殊struts功能的非常简单的JSP页面。列表3-3
    quote.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>
    <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>
    -----------------------------------------------
    上面的JSP中包含简单一个简单的代码行,它用HttpServletRequest获取特定的股票的价格。这个价格数据通过Action对象中的HttpServletRequest实例来设置。创建控制组件
    在Struts应用中,控制器由两个组件组成。这两个组件是org.apache.struts.action.ActionServlet和org.apache. struts.action.Action类。在大多数的Struts应用只存在一个org. apache.struts.action.ActionServlet实现(实例),但存在多个org.apache.struts.action.Action实现(多个继承实例)。org.apache.struts.action.ActionServlet用来控制客户端请求和决定哪个org.apache.struts.action.Action实例来处理请求。在我们创建的简单应用中,缺省的ActionServlet已经足以满足我们的应用需求了,你不需要再实现特定的org.apache.struts.action.ActionServlet。即使需要扩展ActionServlet也很容易。我们会在第四章中详细介绍。
    控制器的第二个组件是org.apache.struts. action.Action,和ActionServlet不同的是Action类在你的应用中必须扩展。这个类是你业务逻辑的开始部分。
    例如我们寻找指定的股票的价格。我们会创建一个从org.apache.struts.action.Action扩展的LookupAction。代码如下:LookupAction.java 
    ------------------------------------------------------------
    package wiley;
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    public class LookupAction extends Action {
    protected Double getQuote(String symbol) {
    if ( symbol.equalsIgnoreCase("SUNW") ) {
    return new Double(25.00);
    }
    return null;
    }
    public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException 
    {
    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);
    }
    // Set the target to failure
    if ( price == null ) {
    target = new String("failure");
    }
    else {
    request.setAttribute("PRICE", price);
    }
    // Forward to the appropriate View
    return (mapping.findForward(target));
    }
    }
    -----------------------------------------------------------------
    我们注意到LookupAction类有两个方法:getQuote() and execute()。getQuote()方法会返回一个价格(如果提交的股票代码为“SUNW”)。execute() 方法是LookupAction的主函数,在所有的Action类中必须被实现(原文可能有误,在strut1.1上不是必须实现,可以使用perform()方法替代)。
      

  2.   

    zf80230901,您好!
    能不能发一份源码给我看看,谢谢![email protected]
      

  3.   

    zf80230901
    用jbuilderx怎莫建structs的环境
    具体一点
    找啦好几天啦
    先new project然后再new web module(war)吗?
    选structs1.02还是structs1.1
    还有
    Runtime configurations中
    Build target 
    type
    Server 都怎么配?
    找啦好几天资料拉,都只些理论。
    写过的朋友点点路啊,十分感谢。
      

  4.   

    发了
    我用的Jb2006
    1.new project
    2.new WebModule
    3.new jsp
    4.new actionForm
    5.new Action
    6.struts-config.xml 
    (图示见邮箱)"选structs1.02还是structs1.1
    还有
    Runtime configurations中
    Build target 
    type
    Server 都怎么配?"一般根据你的需要配置