- 一、 配置web.xml 文档
- 添加模块moduleOne、moduleTwo
- <!-- mainModule -->
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/struts-config.xml</param-value>
- </init-param>
- <!-- moduleOne -->
- <init-param>
- <param-name>config/moduleOne</param-name>
- <param-value>/WEB-INF/moduleOne/struts-moduleOne.xml</param-value>
- </init-param>
- <!-- moduleTwo -->
- <init-param>
- <param-name>config/moduleTwo</param-name>
- <param-value>/WEB-INF/moduleTwo/struts-moduleTwo.xml</param-value>
- </init-param>
- 注:<param-name>格式必须这样写,<param-value>是指在WEB-INF目录下新建moduleOne目录再创建struts-moduleOne.xml,struts-moduleOne.xml格式与struts-config.xml相同。在此同时在WebRoot目录下新建moduleOne和moduleTwo的文件夹,用于区分模块和存放页面文件
- 二、 配置struts-config.xml
- <struts-config>
- <data-sources />
- <form-beans type="org.apache.struts.webapp.CustomFormBean" />
- <global-exceptions />
- <global-forwards
- type="org.apache.struts.webapp.CustomActionForward" />
- <action-mappings
- type="org.apache.struts.webapp.CustomActionMapping">
- <action path="/main" forward="/index.jsp" />
- </action-mappings>
- <message-resources parameter="com.accp.struts.ApplicationResources" />
- </struts-config>注意: type 这三个类是从何而来的呢??答:自定义的
- 所以在配置 struts-config.xml之前我们应创建它们,当然每个类都必须extenx 相应的父类,如下:
- CustomFormBean extenx FormBeanConfig
- CustomActionForward extenx ActionForward
- CustomActionMapping extenx ActionMapping
- 这些类里面可以什么都不写,当然比较好点规范就是在里内添加一个属性,如下:
- private String example =””;
- 相应的get()set()
- 三、 配置子模块
- 经过上面的配置基本已完成,那么我们现在来配置moduleOne子模块信息,添加action、actionFrom、jsp。
- 注意:action 必须extends DispatchAction, jsp页面应放在WebRoot目录下moduleOne
- struts-moduleOne.xml如下:
- <struts-config>
- <data-sources />
- <form-beans>
- <form-bean name="moduleOneForm"
- type="com.accp.struts.form.ModuleOneForm">
- </form-bean>
- </form-beans>
- <global-exceptions />
- <global-forwards>
- <!-- 转发到moduleTwo module指定哪个模块,全局转发 -->
- <forward module="/moduleTwo" redirect="true" name="succeed" path="/displayUser.jsp"/>
- </global-forwards>
- <action-mappings>
- <!-- 主模块index.jsp link导航 -->
- <action path="/reg" forward="/userRegist.jsp"/>
- <action path="/regist" name="moduleOneForm" scope="session" parameter="user"
- type="com.accp.struts.action.ModuleOneAction">
- </action>
- </action-mappings>
- <message-resources parameter="com.accp.struts.ApplicationResources" />
- </struts-config>
- index.jsp 如下:
- <html:link module="/moduleOne" action="/reg.do">到moduleOne的用户注册</html:link>
- 注意:module属性,是指定某个模块,对应的就在某个模块配置<action/>
- userRegist.jsp 如下:
- <html:form action="/regist?user=addUser">
- name : <html:text property="name"/><br/>
- sex : <html:text property="sex"/><br/>
- age : <html:text property="age"/><br/><br/>
- <html:submit value="Submit"/><html:reset value="Reset"/>
- </html:form>
- 注意:user是moduleOne.xml <action parameter="user"……/>因为我们用到了DispatchAction,addUser是指action里的某个方法.
- ModuleOneAction.java如下:
- public class ModuleOneAction extends DispatchAction {
- public ActionForward addUser(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
- ModuleOneForm moduleOneForm = (ModuleOneForm) form;
- System.out.println("name :"+ moduleOneForm.getName());
- System.out.println("age :" + moduleOneForm.getAge());
- System.out.println("sex :" + moduleOneForm.getSex());
- return mapping.findForward("succeed");}}
- displayUser.jsp
- <body>
- name:${moduleOneForm.name}<br/>
- age:${moduleOneForm.age}<br/>
- sex:${moduleOneForm.sex}<br/>
- <html:link action="/main.do">回到主模块</html:link>
- </body>
- 注意:displayUser.jsp 位于moduleTwo目录,<html:link> 没指定module 所以默认从主模块的struts-config.xml寻找配对的<action>
- 四、发布测试…….实现主模块跳转到moduleOne子模块然后导航到另一moduleTwo子模块再导航到主模块
- 阅读时请留意注意部分
阅读时请留意注意部分
在网上看了很多这方面的资料.. 但是. 发现都是很肤浅的一个介绍.
所以在下刚刚做完的一个项目就是用一个多模块的所有跟大家一起分享一下..
大家配置好首先看看他的跳转关系.. 就会很明白他们是怎么一样工作的了
附件清晰版本