Struts是一种MVC框架。
MVC框架实现流程:JspàServletàDAOàServletàJsp
Struts使用ActionServlet实现了MVC中的Servlet的功能,而ActionServlet是由ActionForm和Action共同实现。其中,ActionForm实现了接收表单参数和表单格式验证的功能;Action实现了调用DAO、处理业务逻辑和跳转功能。ActionForm又叫做FormBean,也称为表单对象。
在Web.xml中配置ActionServlet
<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> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> |
在Web.xml中配置了ActionServlet的初始化参数和映射路径,只要是URL为*.do的文件都会调用对应的ActionServlet,然后查找初始化参数中的struts-config.xml文件。
struts-config.xml文件是用来配置Action、FormBean和其它资源的。
<struts-config> <data-sources /> <form-beans > <form-bean name="helloForm" type="com.hym.struts.form.HelloForm" />
</form-beans>
<global-exceptions /> <global-forwards /> <action-mappings > <action attribute="helloForm" input="/hello.jsp" name="helloForm" path="/hello" scope="request" type="com.hym.struts.action.HelloAction"> <forward name="success" path="/helloSuccess.jsp" /> </action>
</action-mappings>
<message-resources parameter="com.hym.struts.ApplicationResources" /> </struts-config> |
其中,<form-beans>与<action-mappings>是必须的。<form-beans>配置FormBean的名称、包.类名。<action-mappings>配置Action,其中的name属性值为本Action使用的FormBean名称,path属性值为“/名称”,例如“/hello”,则所有“/hello.do”的请求都由该Action处理,而“.do”在此时省略;input属性为首页面(或错误页面);<forward />为本Action可能会用到的jsp。
创建struts程序的一般步骤:
1. 新建一个WebPoject项目;
2. 导入struts1.2开发包;
3. 新建一个Jsp页面hello.jsp,注意选择带表单的struts1.2标签支持;然后在hello.jsp中设置表单属性,并设置action属性为“/hello.do”;
4. 新建一个Struts1.2 Form,Action&JSP来创建FormBean和Action,设置一个名为helloForm的FormBean,并添加在hello.jsp中表单的所有属性;点击下一步,点击完成,之后会看到生成HelloForm.java和HelloAction.java两个类生成。
5. 在HelloForm.java中添加表单格式验证代码。
6. 在HelloAction.java中设置跳转方式。
Struts能自动转化常用的数据类型,例如:int、double、boolean、long、java.sql.Date、java.sql.Time、POJO类型。因为在提交表单数据之后,Struts会调用request.getParameter(“”)将值取出,然后通过setter方法设置到同名的FormBean的属性上,如果类型不一致,会进行数据类型的转化。如果是数字类型int、double、float等时,如果格式不对,会当做0。而当变量为Date、Time等复杂类型时,格式不对会抛出异常。
FormBean进行数据校验,处理错误信息:
FormBean中reset()方法用于数据初始化,如果没有提交任何的数据,则执行reset()初始化数据。一般来说,直接范文JSP时,会调用reset()方法。而validate()方法用来提交数据时对数据进行校验。
ActionErrors类似于一个HashMap,如果校验错误,则往ActionErrors里面添加一条ActionMessage记录,如:ActionErrors errors = new ActionErrors ; errors.add(“errName”,new ActionMessge(“name.null”))。当validate()方法返回的ActionErrors为null,或者里面没有任何的ActionMessage记录,则认为通过了校验,并将该FormBean提交给对应的Action运行。如果返回的ActionErrors里面有ActionMessage记录,则认为没有通过校验,数据不会提交给Action,也不会执行Action的execute()方法。
ActionErrors里的信息由Struts的<html:errors property=”errName” />标签显示,errName与errors.add(“errName”,new ActionMessge(“name.null”))中的errName对应。而“name.null”为错误信息的资源索引,而所有错误信息都保存在Struts中的properties资源文件ApplicationResource.properties中。properties资源文件名称也配置在struts-config.xml中,properties资源文件不能保存中文,需要进行ASCII转码,利用JDK自带的native2ascii工具进行转码操作。
对于提交大量数据表单时,FormBean需要设置大量的setter、getter方法,这显然很不方便,从struts 1.2起,FormBean中可以使用POJO,提交数据后将直接把数据赋给POJO对象。由于Struts不会自动创建POJO对象,需要在FormBean中用new实例化一个Person对象,否则为Person设置属性的时候,会抛出异常。
Action是Struts的核心,负责处理实际的业务逻辑。Action是Struts入口ActionServlet的一部分,ActionServlet通过使用反射机制调用Action的execute()方法执行业务代码。
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) |
mapping代表struts-config.xml配置,通过mapping可以获取配置在struts-config.xml中、属于该Action的所有参数。execute()方法返回ActionForward对象。这是一个JSP页面的封装类,代表struts-config.xml中配置的jsp页面,也就是<forward />配置。
<action-mappings > <action attribute="helloForm" input="/hello.jsp" name="helloForm" path="/hello" scope="request" type="com.hym.struts.action.HelloAction" > <forward name="suc" path="/hello_success.jsp"></forward> </action> </action-mappings> |
通过mapping.findForword(“suc”)便可以跳转到hello_success.jsp页面。<action />中的input属性指定的页面是输入页面,通过mapping.getInputForward()获取。