Struts多模块&DispatchAction

 

  1. 一、      配置web.xml 文档    
  2. 添加模块moduleOne、moduleTwo    
  3. <!-- mainModule -->    
  4.     <init-param>    
  5.       <param-name>config</param-name>    
  6.       <param-value>/WEB-INF/struts-config.xml</param-value>    
  7.     </init-param>    
  8.         
  9. <!-- moduleOne -->    
  10.     <init-param>    
  11.       <param-name>config/moduleOne</param-name>    
  12.       <param-value>/WEB-INF/moduleOne/struts-moduleOne.xml</param-value>    
  13.    </init-param>    
  14.         
  15. <!-- moduleTwo -->    
  16.    <init-param>    
  17.     <param-name>config/moduleTwo</param-name>        
  18.     <param-value>/WEB-INF/moduleTwo/struts-moduleTwo.xml</param-value>    
  19.    </init-param>    
  20. 注:<param-name>格式必须这样写,<param-value>是指在WEB-INF目录下新建moduleOne目录再创建struts-moduleOne.xml,struts-moduleOne.xml格式与struts-config.xml相同。在此同时在WebRoot目录下新建moduleOne和moduleTwo的文件夹,用于区分模块和存放页面文件    
  21.   
  22. 二、      配置struts-config.xml    
  23. <struts-config>    
  24.         <data-sources />    
  25.         <form-beans type="org.apache.struts.webapp.CustomFormBean" />    
  26.         <global-exceptions />    
  27.         <global-forwards    
  28.             type="org.apache.struts.webapp.CustomActionForward" />    
  29.         <action-mappings    
  30.            type="org.apache.struts.webapp.CustomActionMapping">    
  31.            <action path="/main" forward="/index.jsp" />    
  32.         </action-mappings>    
  33.         <message-resources parameter="com.accp.struts.ApplicationResources" />    
  34. </struts-config>注意: type 这三个类是从何而来的呢??答:自定义的    
  35. 所以在配置 struts-config.xml之前我们应创建它们,当然每个类都必须extenx 相应的父类,如下:    
  36. CustomFormBean extenx FormBeanConfig    
  37. CustomActionForward extenx ActionForward    
  38. CustomActionMapping extenx ActionMapping    
  39. 这些类里面可以什么都不写,当然比较好点规范就是在里内添加一个属性,如下:    
  40. private String example =””;    
  41. 相应的get()set()    
  42.   
  43. 三、      配置子模块    
  44. 经过上面的配置基本已完成,那么我们现在来配置moduleOne子模块信息,添加action、actionFrom、jsp。    
  45. 注意:action 必须extends DispatchAction, jsp页面应放在WebRoot目录下moduleOne    
  46. struts-moduleOne.xml如下:    
  47. <struts-config>    
  48.         <data-sources />    
  49.         <form-beans>    
  50.            <form-bean name="moduleOneForm"    
  51.                type="com.accp.struts.form.ModuleOneForm">    
  52.            </form-bean>    
  53.         </form-beans>    
  54.         <global-exceptions />    
  55.         <global-forwards>    
  56.             <!-- 转发到moduleTwo module指定哪个模块,全局转发 -->    
  57.         <forward module="/moduleTwo" redirect="true" name="succeed" path="/displayUser.jsp"/>    
  58.         </global-forwards>    
  59.         <action-mappings>    
  60.         <!-- 主模块index.jsp link导航 -->    
  61.          <action path="/reg" forward="/userRegist.jsp"/>    
  62.          <action path="/regist" name="moduleOneForm" scope="session" parameter="user"    
  63.              type="com.accp.struts.action.ModuleOneAction">    
  64.           </action>    
  65.         </action-mappings>    
  66.         <message-resources parameter="com.accp.struts.ApplicationResources" />    
  67. </struts-config>    
  68.   
  69. index.jsp 如下:    
  70. <html:link module="/moduleOne" action="/reg.do">到moduleOne的用户注册</html:link>    
  71. 注意:module属性,是指定某个模块,对应的就在某个模块配置<action/>    
  72.   
  73. userRegist.jsp 如下:    
  74. <html:form action="/regist?user=addUser">    
  75.        name : <html:text property="name"/><br/>    
  76.        sex : <html:text property="sex"/><br/>    
  77.        age : <html:text property="age"/><br/><br/>    
  78.        <html:submit value="Submit"/><html:reset value="Reset"/>    
  79. </html:form>    
  80. 注意:user是moduleOne.xml <action parameter="user"……/>因为我们用到了DispatchAction,addUser是指action里的某个方法.    
  81.   
  82. ModuleOneAction.java如下:    
  83. public class ModuleOneAction extends DispatchAction {    
  84. public ActionForward addUser(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {    
  85.                ModuleOneForm moduleOneForm = (ModuleOneForm) form;    
  86.               System.out.println("name :"+ moduleOneForm.getName());    
  87.                System.out.println("age :" + moduleOneForm.getAge());    
  88.                System.out.println("sex :" + moduleOneForm.getSex());    
  89.                return mapping.findForward("succeed");}}    
  90.   
  91. displayUser.jsp    
  92. <body>    
  93.        name:${moduleOneForm.name}<br/>    
  94.        age:${moduleOneForm.age}<br/>    
  95.        sex:${moduleOneForm.sex}<br/>    
  96.        <html:link action="/main.do">回到主模块</html:link>    
  97. </body>    
  98. 注意:displayUser.jsp 位于moduleTwo目录,<html:link> 没指定module 所以默认从主模块的struts-config.xml寻找配对的<action>    
  99.   
  100. 四、发布测试…….实现主模块跳转到moduleOne子模块然后导航到另一moduleTwo子模块再导航到主模块    
  101. 阅读时请留意注意部分  

阅读时请留意注意部分

在网上看了很多这方面的资料.. 但是. 发现都是很肤浅的一个介绍.

所以在下刚刚做完的一个项目就是用一个多模块的所有跟大家一起分享一下..

大家配置好首先看看他的跳转关系.. 就会很明白他们是怎么一样工作的了

附件清晰版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值