一、在开发时可以采用的一个开发流程如下(引自资料3):
- 收集和定义应用需求。
- 基于数据采集和显示的原则定义和开发"屏幕显示"需求 。
- 为每一个"屏幕显示"定义访问路径。
- 定义ActionMappings建立到应用业务逻辑之间的联系。
- 开发满足"屏幕显示"需求的所有支持对象。
- 基于每一个"屏幕显示"需求提供的数据属性来创建对应的ActionForm对象
- 开发被ActionMapping调用的Action对象。
- 开发应用业务逻辑对象 (Bean,EJB,等等)。
- 对应ActionMapping设计的流程创建JSP页面。
- 建立合适的配置文件struts-config.xml , web.xml。
- 开发/测试/部署
二、具体在使用Struts框架时,对应各个部分的开发工作主要包括:
- Model部分:采用JavaBean和EJB组件,设计和实现系统的业务逻辑。根据不同的请求从Action派生具体Action处理对象。完成"做什么"的任务来调用由Bean构成的业务组件。创建由ActionForm 的派生类实现对客户端表单数据的封装。
- Controller部分:Struts为我们提供了核心控制部分的实现。我们只需要配置ActionMapping对象
- View部分:为了使用Model中的ActionForm 对象,我们必须用Struts提供的自定义标记创建HTML 表单。利用Struts提供的自定义标记库编写用户界面把应用逻辑和显示逻辑分离。Struts框架通过这些自定义标记建立了View和Model之间的联系。Struts的自定义标记还提供了很多定制页面的功能。
- 同时需要编辑两个配置文件:web.xml和struts-config.xml。通过它们配置Struts系统中的各个模块之间的交互。
三、两个配置文件:web.xml和struts-config.xml
web.xml文件的配置:
web应用中的web.xml是第一个要配置的地方,它描述了系统的Controller对象。在web.xml中增加如下标记
<servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>application</param-name> ······ </servlet> |
说明:这个servlet对象就是Struts提供的Controller,还可以为它指定初始化参数,比如对系统应用属性的支持。
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servelt-mapping> |
说明:实现客户请求的url信息和服务器端具体处理的映射关系。
<taglib> <taglib-url>/WEB-INF/struts-bean.tld</taglib-url> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> ······· |
说明:添加对Struts提供的应用所使用的自定义标记库的引用。
struts-config.xml文件的配置:
struts-config.xml是用于建立Controller和Model之间的关系的。它描述了Controller所使用的把请求对应到具体处理的法则,同时它还描述了客户提供的数据与ActionForm组件的对应映射关系。
在struts-config.xml中增加如下标记
<form-beans> <form-bean name="loginForm" type="loginForm" /> </form-beans> |
说明:<form-bean>标记描述一个具体的ActionForm子类对象,通过它和JSP页面中的自定标记的结合使用可以实现ActionForm和View之间的数据映射。
<action-mappings> <action path="/login" type="loginAction" name="loginForm" input="/login.jsp" ··· /> </action-mappings> |
说明:<action-mappings>标记描述了请求和处理的一对一映射关系。input和path属性唯一的标记了客户端的一个请求,name属性描述封装客户端的数据的ActionForm子类对象。Type属性描述处理这个请求的Action子类对象。
通过对两个配置文件的配置,把Struts框架中MVC的各个部分联系起来,实现一个真正的MVC系统
- 关于actionmapping:
Table 3.5 ActionMapping settings
Property Purpose
path A unique identifier for this mapping. It is included in the web address, as in http://localhost:8080/logon/LogonSubmit.do.
type The Action object to call when the path is requested.
name The JavaBean helper (ActionForm) to use with an HTML form.
scope A property that specifies whether to store the helper in the request or the session.
Validate A property that specifies whether to call the standard validate method on the form
bean helper (specified by name) before calling the Action object (specified by type).
input A property that specifies where to send control if the validate method returns false.