Struts 多模块应用

一、配置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-beanstype="org.apache.struts.webapp.CustomFormBean"/>
<global-exceptions/>
<global-forwards
type="org.apache.struts.webapp.CustomActionForward"/>
<action-mappings
type="org.apache.struts.webapp.CustomActionMapping">
<actionpath="/main"forward="/index.jsp"/>
</action-mappings>
<message-resourcesparameter="com.accp.struts.ApplicationResources"/> </struts-config>
注意:type这三个类是从何而来的呢??
答:自定义的所以在配置struts-config.xml之前我们应创建它们,当然每个类都必须extenx相应的父类,如下:
CustomFormBeanextenxFormBeanConfig
CustomActionForwardextenxActionForward
CustomActionMappingextenxActionMapping
这些类里面可以什么都不写,当然比较好点规范就是在里内添加一个属性,
如下:
privateStringexample="";
相应的get()set()

三、配置子模块
经过上面的配置基本已完成,那么我们现在来配置moduleOne子模块信息,添加action、actionFrom、jsp。
注意:action必须extendsDispatchAction,jsp页面应放在WebRoot目录下moduleOne
struts-moduleOne.xml如下:
<struts-config>
<data-sources/>
<form-beans>
<form-beanname="moduleOneForm"
type="com.accp.struts.form.ModuleOneForm">
</form-bean>
</form-beans>
<global-exceptions/>
<global-forwards>
<!--转发到moduleTwomodule指定哪个模块,全局转发-->
<forwardmodule="/moduleTwo"redirect="true"name="succeed"path="/displayUser.jsp"/>
</global-forwards>
<action-mappings>
<!--主模块index.jsplink导航-->
<actionpath="/reg"forward="/userRegist.jsp"/>
<actionpath="/regist"name="moduleOneForm"scope="session"parameter="user"
type="com.accp.struts.action.ModuleOneAction">
</action>
</action-mappings>
<message-resourcesparameter="com.accp.struts.ApplicationResources"/>
</struts-config>

index.jsp如下:
<html:linkmodule="/moduleOne"action="/reg.do">到moduleOne的用户注册</html:link>
注意:module属性,是指定某个模块,对应的就在某个模块配置<action/>

userRegist.jsp如下:
<html:formaction="/regist?user=addUser">
name:<html:textproperty="name"/><br/>
sex:<html:textproperty="sex"/><br/>
age:<html:textproperty="age"/><br/><br/>
<html:submitvalue="Submit"/><html:resetvalue="Reset"/>
</html:form>
注意:user是moduleOne.xml<actionparameter="user"....../>因为我们用到了DispatchAction,addUser是指action里的某个方法.

ModuleOneAction.java如下:
publicclassModuleOneActionextendsDispatchAction{
publicActionForwardaddUser(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse){
ModuleOneFormmoduleOneForm=(ModuleOneForm)form;
System.out.println("name:"+moduleOneForm.getName());
System.out.println("age:"+moduleOneForm.getAge());
System.out.println("sex:"+moduleOneForm.getSex());
returnmapping.findForward("succeed");}}
displayUser.jsp
<body>
name:${moduleOneForm.name}<br/>
age:${moduleOneForm.age}<br/>
sex:${moduleOneForm.sex}<br/>
<html:linkaction="/main.do">回到主模块</html:link>
</body>
注意:displayUser.jsp位于moduleTwo目录,<html:link>没指定module所以默认从主模块的struts-config.xml寻找配对的<action>

你可能感兴趣的:(html,xml,jsp,Web,struts)