| ||||||
本章讲解了一个简单的Struts应用例子helloapp应用,这个例子可以帮助读者迅速入门,获得开发Struts应用的基本经验。该应用的功能非常简单,接受用户输入的姓名<name>,然后输出“Hello <name>”。开发helloapp应用涉及以下内容: l 分析应用需求 l 把基于MVC设计模式的Struts框架运用到应用中 l 创建视图组件,包括HTML表单(hello.jsp)和ActionForm Bean(HelloForm.java) l 创建application.properties资源文件 l 数据验证,包括表单验证和业务逻辑验证 l 创建控制器组件: HelloAction.java l 创建模型组件: PersonBean.java l 创建包含被各个模块共享的常量数据的Java文件: Constants.java l 创建配置文件:web.xml和struts-config.xml l 编译、发布和运行helloapp应用 2.1 分析helloapp应用的需求在开发应用时,首先从分析需求入手,列举该应用的各种功能,以及限制条件。helloapp应用的需求非常简单,包括如下需求: l 接受用户输入的姓名<name>,然后返回字符串“Hello <name> !” l 如果用户没有输入姓名就提交表单,将返回出错信息,提示用户首先输入姓名。 l 如果用户输入姓名为“Monster”,将返回出错信息,拒绝向“Monster”打招呼。 l 为了演示模型组件的功能,本应用使用模型组件来保存用户输入的姓名。 2.2 运用Struts框架下面把Struts框架运用到helloapp应用中。Struts框架可以方便迅速的把一个复杂的应用划分成模型、视图和控制器组件,而Struts的配置文件struts-config.xml则可以灵活的组装这些组件,简化开发过程。 以下是helloapp应用的各个模块的构成: l 模型包括一个JavaBean组件PersonBean,它有一个userName属性,代表用户输入的名字。它提供了get/set方法,分别用于读取和设置userName属性,它还提供一个save()方法,负责把userName属性保存到持久化存储系统中,如数据库或文件系统。对于更为复杂的Web应用,JavaBean组件可以作为EJB或Web服务的前端组件。 l 视图包括一个JSP文件hello.jsp,它提供用户界面,接受用户输入的姓名。视图还包括一个ActionForm Bean,它用来存放表单数据,并进行表单验证,如果用户没有输入姓名就提交表单,将返回出错信息。 l 控制器包括一个Action类HelloAction,它完成三项任务:1.进行业务逻辑验证,如果用户输入的姓名为“Monster”, 将返回错误消息;2.调用模型组件PersonBean的save()方法,保存用户输入的名字;3.决定将合适的视图组件返回给用户。
除了创建模型、视图和控制器组件,还需要创建Struts的配置文件struts-config.xml,它可以把这些组件组装起来,使它们协调工作。此外,还需要创建整个Web应用的配置文件web.xml。 2.3 创建视图组件本例中,视图包括两个组件: l 一个JSP文件:hello.jsp l 一个ActionForm Bean: HelloForm Bean
下面分别讲述如何创建这两个组件。 2.3.1 创建JSP文件hello.jsp提供用户界面,能够接受用户输入的姓名。此外,本Web应用的所有输出结果也都由hello.jsp显示给用户。图2-1显示了hello.jsp提供的网页。 在图2-1中,用户输入姓名“Weiqin”后,按提交表单,本应用将返回“Hello Weiqin!”,参见图2-2。
图2-2 hello.jsp接受用户输入后正常返回的网页
例程2-1为hello.jsp文件的源代码。 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html locale="true"> <head> <title><bean:message key="hello.jsp.title"/></title> <html:base/> </head>
<body bgcolor="white"><p> <h2><bean:message key="hello.jsp.page.heading"/></h2><p> <html:errors/><p>
<logic:present name="personbean" scope="request"> <h2> <bean:message key="hello.jsp.page.hello"/> <bean:write name="personbean" property="userName" />!<p> </h2> </logic:present>
<html:form action="/HelloWorld.do" focus="userName" > <bean:message key="hello.jsp.prompt.person"/> <html:text property="userName" size="16" maxlength="16"/><br> <html:submit property="submit" value="Submit"/> <html:reset/>
</html:form><br>
<html:img page="/struts-power.gif" alt="Powered by Struts"/> </body> </html:html> 以上基于Struts框架的JSP文件有以下特点: l 没有任何Java程序代码 l 使用了许多Struts的客户化标签,例如<html:form>和<logic:present>标签 l 没有直接提供文本内容,取而代之的是<bean:message>标签,输出到网页上的文本内容都是由<bean:message>标签来生成的。例如: <bean:message key="hello.jsp.prompt.person"/>
Struts客户化标签是联系视图组件和Struts框架中其它组件的纽带。这些标签可以访问或显示来自于控制器和模型组件的数据。在本书第12章至16章讲专门介绍Struts标签的用法,本节先简单介绍几种重要的Struts标签。
hello.jsp开头几行用于声明和加载Struts标签库: <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
以上代码表明该JSP文件使用了Struts Bean、Html和Logic 标签库,这是加载客户化标签库的标准JSP语法。 hello.jsp中使用了来自 Struts HTML标签库中的标签,包括<html:errors>, <html:form>和<html:text>: l <html:errors>:用于显示Struts框架中其他组件产生的错误消息。 l <html:form>:用于创建HTML表单,它能够把HTML表单的字段和ActionForm Bean的属性关联起来。 l <html:text>:该标签是<html:form>的子标签,用于创建HTML表单的文本框。它和ActionForm Bean的属性相关联。
hello.jsp中使用了来自Struts Bean标签库的两个标签<bean:message>和<bean:write> : l <bean:message>:用于输出本地化的文本内容,它的key属性指定消息key,和消息key匹配的文本内容来自于专门的Resource Bundle,关于Resource Bundle的概念参见本书第9章(Struts应用的国际化)。 l <bean:write>:用于输出JavaBean的属性值。本例中,它用于输出personbean对象的userName属性值: <bean:write name="personbean" property="userName" />
hello.jsp使用了来自Struts Logic标签库的<logic:present>标签。<logic:present>标签用来判断JavaBean在特定的范围内是否存在,只有当JavaBean存在,才会执行标签主体中的内容: <logic:present name="personbean" scope="request"> <h2> Hello <bean:write name="personbean" property="userName" />!<p> </h2> </logic:present> 本例中,<logic:present>标签用来判断在request范围内是否存在personbean对象,如果存在,就输出personbean的userName属性值。和<logic:present>标签相对的是<logic:notPresent>标签,它表示只有当JavaBean在特定的范围内不存在,才会执行标签主体中的内容。 2.3.2 创建消息资源文件hello.jsp使用<bean:message>标签来输出文本内容。这些文本来自于Resource Bundle,每个Resource Bundle都对应一个或多个本地化的消息资源文件,本例中的资源文件为application.properties,例程2-2是该消息资源文件的内容。 例程 2-2 application.properties文件 #Application Resources for the "Hello" sample application hello.jsp.title=Hello - A first Struts program hello.jsp.page.heading=Hello World! A first Struts application hello.jsp.prompt.person=Please enter a UserName to say hello to : hello.jsp.page.hello=Hello
#Validation and error messages for HelloForm.java and HelloAction.java hello.dont.talk.to.monster=We don't want to say hello to Monster!!! hello.no.username.error=Please enter a <i>UserName</i> to say hello to! 以上文件以“消息key/消息文本”的格式存放数据,文件中“#”后面为注释行。对于以下JSP代码: <bean:message key="hello.jsp.title"/> <bean:message>标签的key属性为“hello.jsp.tilte”,在Resource Bundle中与之匹配的内容为: hello.jsp.title=Hello - A first Struts program 因此,以上<bean:message>标签将把“Hello - A first Struts program”输出到网页上。 2.3.3 创建ActionForm Bean当用户提交了HTML表单,Struts框架自动把表单数据组装到ActionForm Bean中。ActionForm Bean中的属性和HTML表单中的字段一一对应。ActionForm Bean还提供数据验证方法,以及把属性重新设置为默认值的方法。Struts框架中定义的ActionForm类是抽象的,必须在应用中创建它的子类,来存放具体的HTML表单数据。例程2-3为HelloForm.java的源程序, 它用于处理hello.jsp中的表单数据。 package hello;
import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping;
public final class HelloForm extends ActionForm {
private String userName = null;
public String getUserName() { return (this.userName); }
public void setUserName(String userName) { this.userName = userName; }
/** * Reset all properties to their default values. */ public void reset(ActionMapping mapping, HttpServletRequest request) { this.userName = null; }
/** * Validate the properties posted in this request. If validation errors are * found, return an <code>ActionErrors</code> object containing the errors. * If no validation errors occur, return <code>null</code> or an empty * <code>ActionErrors</code> object. */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((userName == null) || (userName.length() < 1)) errors.add("username", new ActionMessage("hello.no.username.error"));
return errors; } } 从以上代码可以看出,ActionForm Bean实质上是一种JavaBean,不过它除了具有JavaBean的常规方法,还有两个特殊方法: l validate():用于表单验证。 l reset():把属性重新设置为默认值。 2.3.4 数据验证几乎所有和用户交互的应用都需要数据验证,而从头设计并开发完善的数据验证机制往往很费时。幸运的是,Struts框架提供了现成的、易于使用的数据验证功能。Struts框架的数据验证可分为两种类型:表单验证和业务逻辑验证,在本例中,它们分别运用于以下场合: l 表单验证:如果用户没有在表单中输入姓名,就提交表单,将生成表单验证错误 l 业务逻辑验证:如果用户在表单中输入的姓名为“Monster”,按照本应用的业务规则,不允许向“Monster”打招呼,因此将生成业务逻辑错误。
第一种类型的验证,即表单验证由ActionForm Bean来负责处理。在本例中,HelloForm.java的validate()方法负责完成这一任务: public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors();
if ((userName == null) || (userName.length() < 1)) errors.add("username", new ActionMessage("hello.no.username.error"));
return errors; } } 当用户提交了HTML表单,Struts框架自动把表单数据组装到ActionForm Bean中。接下来Struts框架会自动调用ActionForm Bean的validate()方法进行表单验证。如果validate()方法返回的ActionErrors 对象为null,或者不包含任何ActionMessage对象,就表示没有错误,数据验证通过。如果ActionErrors中包含ActionMessage对象,就表示发生了验证错误,Struts框架会把ActionErrors对象保存到request范围内,然后把请求转发到恰当的视图组件,视图组件通过<html:errors>标签把request范围内的ActionErrors对象中包含的错误消息显示出来,提示用户修改错误。
第二种类型的验证,即业务逻辑验证,由Action来负责处理,参见本章 2.4.3 节。 2.4 创建控制器组件控制器组件包括ActionServlet类和Action类。ActionServlet类是Struts框架自带的,它是整个Struts框架的控制枢纽,通常不需要扩展。Struts框架提供了可供扩展的Action类,它用来处理特定的HTTP请求,例程2-4为HelloAction类的源程序。 package hello;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.util.MessageResources;
public final class HelloAction extends Action {
/** * Process the specified HTTP request, and create the corresponding HTTP * response (or forward to another web component that will create it). * Return an <code>ActionForward</code> instance describing where and how * control should be forwarded, or <code>null</code> if the response has * already been completed. */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// These "messages" come from the ApplicationResources.properties file MessageResources messages = getResources(request);
/* * Validate the request parameters specified by the user * Note: Basic field validation done in HelloForm.java * Business logic validation done in HelloAction.java */ ActionMessages errors = new ActionMessages(); String userName = (String)((HelloForm) form).getUserName();
String badUserName = "Monster";
if (userName.equalsIgnoreCase(badUserName)) { errors.add("username", new ActionMessage("hello.dont.talk.to.monster", badUserName )); saveErrors(request, errors); return (new ActionForward(mapping.getInput())); }
/* * Having received and validated the data submitted * from the View, we now update the model */ PersonBean pb = new PersonBean(); pb.setUserName(userName); pb.saveToPersistentStore();
/* * If there was a choice of View components that depended on the model * (or some other) status, we'd make the decision here as to which * to display. In this case, there is only one View component. * * We pass data to the View components by setting them as attributes * in the page, request, session or servlet context. In this case, the * most appropriate scoping is the "request" context since the data * will not be neaded after the View is generated. * * Constants.PERSON_KEY provides a key accessible by both the * Controller component (i.e. this class) and the View component * (i.e. the jsp file we forward to). */
request.setAttribute( Constants.PERSON_KEY, pb);
// Remove the Form Bean - don't need to carry values forward request.removeAttribute(mapping.getAttribute());
// Forward control to the specified success URI return (mapping.findForward("SayHello")); } }
HelloAction.java是本应用中最复杂的程序,下面分步讲解它的工作机制和流程。 2.4.1 A ction类的工作机制所有的Action类都是org.apache.struts.action.Action的子类。Action子类应该覆盖父类的execute() 方法。当ActionForm Bean被创建,并且表单验证顺利通过后, Struts框架就会调用Action类的execute()方法。execute()方法的定义如下:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException ;
execute()方法包含以下参数: l ActionMapping:包含了这个Action的配置信息,和struts-config.xml文件中的<action>元素对应。 l ActionForm:包含了用户的表单数据,当Struts框架调用execute()方法时,ActionForm中的数据已经通过了表单验证。 l HttpServletRequest:当前的HTTP请求对象 l HttpServletResponse:当前的HTTP响应对象
Action类的execute()方法返回ActionForward对象,它包含了请求转发路径信息。 2.4.2 访问封装在MessageResources中的本地化文本在本例中,Action类的execute()方法首先获得MessageResources对象: MessageResources messages = getResources(request);
在Action类中定义了getResources(HttpServletRequest request)方法,该方法返回当前默认的MessageResources对象,它封装了Resource Bundle中的文本内容。接下来Action类就可以通过MessageResources对象来访问文本内容。例如,如果要读取消息key为“hello.jsp.title”对应的文本内容,可以调用MessageResources类的getMessage(String key)方法: String title=messages.getMessage("hello.jsp.title"); 2.4.3 业务逻辑验证接下来,Action类的execute()方法执行业务逻辑验证: ActionMessages errors = new ActionMessages(); String userName = (String)((HelloForm) form).getUserName();
String badUserName = "Monster";
if (userName.equalsIgnoreCase(badUserName)) { errors.add("username", new ActionMessage("hello.dont.talk.to.monster", badUserName )); saveErrors(request, errors); return (new ActionForward(mapping.getInput())); } 如果用户输入的姓名为“Monster”,将创建包含错误信息的ActionMessage对象,ActionMessage对象被保存到ActionMessages对象中。接下来调用在Action基类中定义的saveErrors()方法,它负责把ActionMessages对象保存到request范围内。最后返回ActionForward对象,Struts框架会根据ActionForward对象包含的转发信息把请求转发到恰当的视图组件,视图组件通过<html:errors>标签把request范围内的ActionMessages对象中包含的错误消息显示出来,提示用户修改错误。 在 2.3.4 节还提到了ActionErrors对象,图2-3显示了ActionMessages、ActionErrors、ActionMessage和ActionError类的类框图。ActionErrors继承ActionMessages,ActionError继承ActionMessage,ActionMessages和ActionMessage之间为聚集关系,即一个ActionMessages对象中可以包含多个ActionMessage对象。
图2-3 ActionMessages、ActionErrors、ActionMessage和ActionError类的类框图 表单验证通常只对用户输入的数据进行简单的语法和格式检查,而业务逻辑验证会对数据进行更为复杂的验证,很多情况下,需要模型组件的介入,才能完成业务逻辑验证。 2.4.4 访问模型组件接下来,HelloAction类创建了一个模型组件PersonBean对象,并调用它的saveTopersistentStore()方法来保存userName属性: PersonBean pb = new PersonBean(); pb.setUserName(userName); pb.saveToPersistentStore(); 本例仅提供了Action类访问模型组件简单的例子。在实际应用中,Action类会访问模型组件,完成更加复杂的功能,例如: l 从模型组件中读取数据,用于被视图组件显示 l 和多个模型组件交互 l 依据从模型组件中获得的信息,来决定返回哪个视图组件 2.4.5 向视图组件传递数据Action类把数据存放在request或session范围内,以便向视图组件传递信息。以下是 HelloAction.java向视图组件传递数据的代码: request.setAttribute( Constants.PERSON_KEY, pb);
// Remove the Form Bean - don't need to carry values forward request.removeAttribute(mapping.getAttribute());
以上代码完成两件事: l 把PersonBean对象保存在request范围内。 l 从request范围内删除ActionForm Bean。由于后续的请求转发目标组件不再需要HelloForm Bean,所以可将它删除。
2.4.6 把HTTP请求转发给合适的视图组件最后,Action类把流程转发给合适的视图组件。 // Forward control to the specified success URI return (mapping.findForward("SayHello")); 2.5 创建模型组件在上一节已经讲过,Action类会访问模型组件。本例中模型组件为一JavaBean: PersonBean。例程2-5是PersonBean的源代码: package hello; public class PersonBean {
private String userName = null;
public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; }
/** * This is a stub method that would be used for the Model to save * the information submitted to a persistent store. In this sample * application it is not used. */ public void saveToPersistentStore() {
/* * This is a stub method that might be used to save the person's * name to a persistent store(i.e. database) if this were a real application. * * The actual business operations that would exist within a Model * component would depend upon the requirements of the application. */ } }
PersonBean是一个非常简单的JavaBean,它包括一个userName属性,以及相关的get/set方法。此外,它还有一个业务方法saveToPersistentStore()。本例中并没有真正实现这一方法。在实际应用中,这个方法可以用来把JavaBean的属性保存在持久化存储系统中,如数据库或文件系统。 通过这个简单的例子,读者可以进一步理解Struts框架中使用模型组件的一大优点,它把业务逻辑的实现和应用的其他部分分离开来,可以提高整个应用的灵活性、可重用性和可扩展性。如果模型组件的实现发生改变,例如本来把JavaBean的属性保存在MySQL数据库中,后来改为保存在Oracle数据库中,此时Action类不需要作任何变动。不仅如此,即使模型组件由JavaBean改为EJB,运行在远程应用服务器上,也不会对Action类造成任何影响。 2.6 创建存放常量的Java文件根据 2.4.5 小节,HelloAction类和视图组件之间通过HttpServletRequest的setAttribute()和getAttribute()方法来共享request范围内的数据。下面再看一下HelloAction类调用HttpServletRequest的setAttribute()方法的细节。 当HelloAction类调用HttpServletRequest的setAttribute()方法,向hello.jsp传递PersonBean对象时,需要提供一个名为“personbean”的属性key: request.setAttribute("personbean",pb);
hello.jsp通过这个名为“personbean”的属性key来读取PersonBean对象: <logic:present name="personbean" scope="request"> <h2> Hello <bean:write name="personbean" property="userName" />!<p> </h2> </logic:present>
对于Struts应用,提倡将这些属性key常量定义在一个Java文件Constants.java中,例程2-6显示了它的源程序。 package hello; public final class Constants { /** * The application scope attribute under which our user database * is stored. */ public static final String PERSON_KEY = "personbean"; }
这样,HelloAction类可以按以下方式来调用HttpServletRequest的setAttribute()方法: request.setAttribute( Constants.PERSON_KEY, pb);
把一些常量定义在Constants.java中可以提高Action类的独立性,当属性key常量值发生改变,只需要修改Constants.java文件,不需要修改Action类。 此外,在本例中,把PersonBean对象保存在HttpServletRequest对象中。对于其他实际的Web应用,也可以根据需要把JavaBean对象保存在HttpSession对象中。 2.7 创建配置文件2.7.1 创建Web应用的配置文件对于Struts应用,它的配置文件web.xml应该对ActionServlet类进行配置,此外,还应该声明Web应用所使用的Struts标签库,本例中声明使用了三个标签库: Struts Bean、Struts HTML和Struts Logic标签库。例程2-7为web.xml的源代码。 例程2-7 web.xml <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app> <display-name>HelloApp Struts Application</display-name>
<!-- Standard Action Servlet Configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>
<!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
<!-- The Usual Welcome File List --> <welcome-file-list> <welcome-file>hello.jsp</welcome-file> </welcome-file-list>
<!-- Struts Tag Library Descriptors --> <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib>
<taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib>
<taglib> <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib>
</web-app>
2.7.2 创建Struts框架的配置文件正如前面提及的,Struts框架允许把应用划分成多个组件,提高开发速度。而Struts框架的配置文件struts-config.xml可以把这些组件组装起来,决定如何使用它们。例程2-8是helloapp应用的struts-config.xml文件的源代码。 <?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<!-- This is the Struts configuration file for the "Hello!" sample application -->
<struts-config>
<!-- ======== Form Bean Definitions =================================== --> <form-beans> <form-bean name="HelloForm" type="hello.HelloForm"/> </form-beans>
<!-- ========== Action Mapping Definitions ============================== --> <action-mappings> <!-- Say Hello! --> <action path = "/HelloWorld" type = "hello.HelloAction" name = "HelloForm" scope = "request" validate = "true" input = "/hello.jsp" > <forward name="SayHello" path="/hello.jsp" /> </action> </action-mappings>
<!-- ========== Message Resources Definitions =========================== --> <message-resources parameter="hello.application"/>
</struts-config>
以上代码对helloapp应用的HelloForm、HelloAction和消息资源文件进行了配置,首先通过<form-bean>元素配置了一个ActionForm Bean,名叫HelloForm,它对应的类为hello.HelloForm: <form-bean name="HelloForm" type="hello.HelloForm"/> 接着通过<action>元素配置了一个Action组件: <action path = "/HelloWorld" type = "hello.HelloAction" name = "HelloForm" scope = "request" validate = "true" input = "/hello.jsp" > <forward name="SayHello" path="/hello.jsp" /> </action>
<action>元素的path属性指定请求访问Action的路径,type属性指定Action的完整类名,name属性指定需要传递给Action的ActionForm Bean,scope属性指定ActionForm Bean的存放范围,validate属性指定是否执行表单验证,input属性指定当表单验证失败时的转发路径。<action>元素还包含一个<forward>子元素,它定义了一个请求转发路径。 本例中的<action>元素配置了HelloAction组件,对应的类为hello.HelloAction,请求访问路径为“HelloWorld”,当Action类被调用时,Struts框架应该把已经包含表单数据的HelloForm Bean传给它。HelloForm Bean存放在request范围内,并且在调用Action类之前,应该进行表单验证。如果表单验证失败,请求将被转发到接收用户输入的网页hello.jsp,让用户纠正错误。
struts-config.xml文件最后通过<message-resources>元素定义了一个Resource Bundle: <message-resources parameter="hello.application"/>
<message-resources>元素的parameter属性指定Resource Bundle使用的消息资源文件。本例中parameter属性为“hello.application”,表明消息资源文件名为“application.properties”,它的存放路径为WEB-INF/classes/hello/application.properties。 2.8 发布和运行helloapp应用helloapp应用作为Java Web应用,它的目录结构应该符合Sun公司制定的Java Web应用的规范,此外,由于helloapp应用使用了Struts框架,因此应该把Struts框架所需的JAR文件和标签库描述文件TLD文件包含进来。访问http://jakarta.apache.org/builds,可以下载最新的Struts软件包,把struts压缩文件解压后,在其lib子目录下提供了Struts框架所需的JAR文件: l commons-beanutils.jar l commons-collections.jar l commons-digester.jar l commons-fileupload.jar l commons-logging.jar l commons-validator.jar l jakarta-oro.jar l struts.jar
在Struts软件包的lib子目录下还提供了所有的Struts标签库描述TLD文件: l struts-bean.tld l struts-html.tld l struts-logic.tld l struts-nested.tld l struts-tiles.tld
图2-4显示了helloapp应用的目录结构。
图2-4 helloapp应用的目录结构
helloapp应用的Java源文件位于helloapp/src目录下,编译这些Java源文件时,应该把Servlet API的JAR文件以及Struts的struts.jar文件加到classpath中。如果在本地安装了Tomcat服务器,假定Tomcat的根目录为<CATALINA_HOME>,在<CATALINA_HOME>/common/lib目录下提供了servlet-api.jar文件。 在本书配套光盘的sourcecode/helloapp/version1/helloapp目录下提供了该应用的所有源文件,只要把整个helloapp子目录拷贝到<CATALINA_HOME>/webapps下,就可以按开放式目录结构发布这个应用。 如果helloapp应用开发完毕,进入产品发布阶段,应该将整个Web应用打包为WAR文件,再进行发布。在本例中,也可以按如下步骤在Tomcat服务器上发布helloapp应用。
(1)在DOS下转到helloapp应用的根目录。 (2)把整个Web应用打包为helloapp.war文件,命令如下: jar cvf helloapp.war *.* (3)把helloapp.war文件拷贝到<CATALINA_HOME>/webapps目录下。 (4)启动Tomcat服务器。Tomcat服务器启动时,会把webapps目录下的所有WAR文件自动展开为开放式的目录结构。所以服务器启动后,会发现服务器把helloapp.war展开到<CATALINA_HOME> /webapps/helloapp目录中。 (5)通过浏览器访问http://localhost:8080/helloapp/hello.jsp。 2.8.1 服务器端装载hello.jsp的流程在Tomcat服务器上成功发布了helloapp应用后,访问http://localhost:8080/helloapp/hello.jsp,会看到如图2-5所示的网页。服务器端装载hello.jsp网页的流程如下。 (1)<bean:message>标签从Resource Bundle中读取文本,把它输出到网页上。 (2)<html:form>标签在request范围中查找HelloForm Bean。如果存在这样的实例,就把HelloForm对象中的userName属性赋值给HTML表单的userName文本框。由于此时还不存在HelloForm对象,所以忽略这项操作。 (3)把hello.jsp的视图呈现给客户。
图2-5 直接访问hello.jsp的输出网页 2.8.2 表单验证的流程在hello.jsp网页上,不输入姓名,直接单击【Submit】按钮,会看到如图2-6所示的网页。
图2-6 表单验证失败的hello.jsp网页 当客户提交HelloForm表单时,请求路径为“/HelloWorld.do”: <html:form action="/HelloWorld.do" focus="userName" > 服务器端执行表单验证流程如下。 (1)Servlet容器在web.xml文件中寻找<url-pattern>属性为“*.do”的<servlet-mapping>元素: <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> (2)Servlet容器依据以上<servlet-mapping>元素的<servlet-name>属性“action”,在web.xml文件中寻找匹配的<servlet>元素: <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> </servlet> (3)Servlet容器把请求转发给以上<servlet>元素指定的ActionServlet,ActionServlet依据用户请求路径“/HelloWorld.do”,在Struts配置文件中检索path属性为“/HelloWorld”的<action>元素: <action path = "/HelloWorld" type = "hello.HelloAction" name = "HelloForm" scope = "request" validate = "true" input = "/hello.jsp" > <forward name="SayHello" path="/hello.jsp" /> </action>
(4)ActionServlet根据<action>元素的name属性,创建一个HelloForm对象,把客户提交的表单数据传给HelloForm对象,再把HelloForm对象保存在<action>元素的scope属性指定的request范围内。 (5)由于<action>元素的validate属性为true,ActionServlet调用HelloForm对象的validate()方法执行表单验证: public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors();
if ((userName == null) || (userName.length() < 1)) errors.add("username", new ActionMessage("hello.no.username.error"));
return errors; } (6)HelloForm对象的validate()方法返回一个ActionErrors对象,里面包含一个ActionMessage对象,这个ActionMessage对象中封装了错误消息,消息key为“hello.no.username.error”,在Resource Bundle中与值匹配的消息文本为: hello.no.username.error=Please enter a <i>UserName</i> to say hello to!
(7)ActionServlet把HelloForm的validate()方法返回的ActionErrors对象包存在request范围内,然后根据<action>元素的input属性,把客户请求转发给hello.jsp。 (8)hello.jsp的<html:errors>标签从request范围内读取ActionErrors对象,再从ActionErrors对象中读取ActionMessage对象,把它包含的错误消息显示在网页上。 2.8.3 逻辑验证失败的流程接下来在hello.jsp的HTML表单中输入姓名“Monster”,然后单击【Submit】按钮。当服务器端响应客户请求时,验证流程如下。 (1)重复 2.8.2 节的步骤1至4。 (2)ActionServlet调用HelloForm对象的validate()方法,这次validate()方法返回的ActionErrors对象中不包含任何ActionMessage对象,表示表单验证成功。 (3)ActionServlet查找HelloAction实例是否存在,如果不存在就创建一个实例。然后调用HelloAction的execute()方法。 (4)HelloAction的execute()方法先进行逻辑验证,由于没有通过逻辑验证,就创建一个ActionMessage对象,这个ActionMessage对象封装了错误消息,消息key为“hello.dont.talk.to.monster”,在Resource Bundle中与值匹配的消息文本为: hello.dont.talk.to.monster=We don't want to say hello to Monster!!!
execute()方法把ActionMessage对象保存在ActionMessages对象中,再把ActionMessages对象存放在request范围内。最后返回一个ActionForward对象,该对象包含的请求转发路径为<action>元素的input属性指定的hello.jsp。 以下是execute()方法中进行逻辑验证的代码: ActionMessages errors = new ActionMessages(); String userName = (String)((HelloForm) form).getUserName(); String badUserName = "Monster";
if (userName.equalsIgnoreCase(badUserName)) { errors.add("username", new ActionMessage("hello.dont.talk.to.monster", badUserName )); saveErrors(request, errors); return (new ActionForward(mapping.getInput())); }
(5)ActionServlet依据HelloAction返回的ActionForward对象,再把请求转发给hello.jsp。 (6)hello.jsp的<html:errors>标签从request范围内读取ActionMessages对象,再从ActionMessages对象中读取ActionMessage对象,把它包含的错误消息显示在网页上。如图2-7所示。
图2-7 逻辑验证失败时的hello.jsp网页 2.8.4 逻辑验证成功的流程接下来,在hello.jsp的HTML表单中输入姓名“Weiqin”,然后单击【Submit】按钮。当服务器端响应客户请求时,流程如下。 (1)重复 2.8.3 节的步骤1至3。 (2)HelloAction的execute()方法先执行逻辑验证,这次通过了验证,然后执行相关的业务逻辑,最后调用ActionMapping.findForward()方法,参数为“SayHello”: // Forward control to the specified success URI return (mapping.findForward("SayHello")); (3)ActionMapping.findForward()方法从<action>元素中寻找name属性为“SayHello”的<forward>子元素,然后返回与之对应的ActionForward对象,它代表的请求转发路径为“/hello.jsp”。
(4)HelloAction的execute()方法然后把ActionForward对象返回给ActionServlet,ActionServlet再把客户请求转发给hello.jsp。 (5)hello.jsp的<bean:message>标签从Resource Bundle中读取文本,把它们输出到网页上,最后生成动态网页,如图2-8所示。
图2-8 通过数据验证的hello.jsp网页 2.9 小节本章通过简单完整的helloapp应用例子,演示了如何把Struts框架运用到Web应用的开发中。通过这个例子,读者可以掌握以下内容: l 分析应用需求,把应用分解为模型、视图和控制器来实现这些需求。 l 利用Struts的标签库来创建视图组件。视图组件中的文本内容保存在专门的消息资源文件中,在JSP文件中通过Struts的<bean:message>标签来访问它,这样可以很方便的实现Struts应用的国际化,支持多国语言。 l Struts框架采用ActionForm Bean把视图中的表单数据传给控制器组件。ActionForm Bean被存放在request或session范围内,它能够被JSP组件、Struts标签以及Action类共享。 l 数据验证分为两种类型:HTML表单验证和业务逻辑验证。表单验证由ActionForm Bean的validate()方法来实现。业务逻辑验证由Action类或模型组件来实现。 l ActionMessage可以表示数据验证错误,它被保存在ActionMessages(或其子类ActionErrors)集合对象中。ActionMessages对象被保存在request范围内,Struts的视图组件可以通过<html:errors>标签来访问它。 l Action类的execute()方法调用模型组件来完成业务逻辑,它还能决定把客户请求转发给哪个视图组件。 l 模型组件具有封装业务实现细节的功能,开发者可以方便的把模型组件移植到远程应用服务器上,这不会对MVC的其它模块造成影响。 l 通过调用HttpServletRequest或HttpSession的setAttribute()以及getAttribute()方法,可以保存或访问在request或session范围内的Java对象,从而实现视图组件和控制器组件之间信息的交互与共享。 l 利用struts-config.xml文件来配置Struts应用。 |
Struts 入门
最新推荐文章于 2020-01-09 11:07:31 发布