Struts多模块应用的配置
--------------------------------------------------------------------------------
建立一个简单的多模块应用"Moduler"
PHP 代码:
Moduler
-index.jsp
-module1
-index.jsp
-module2
-index.jsp
-WEB-INF
-struts-*.tld
-struts-config.xml
-web.xml
-lib
-*.jar
-module1
-struts-config.xml
-module2
-struts-config.xml
-classes
-your_package
web-inf下面的struts-config.xml是struts应用默认配置
对子模块的配置文件可以放在任意的位置,为了让Tomcat能够找到它们的位置,你需要在web.xml中配置
<!-- module configurations -->
<init-param>
<param-name>config/module1</param-name>
<param-value>/WEB-INF/module1/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>config/module2</param-name>
<param-value>/WEB-INF/module2/struts-config.xml</param-value>
</init-param>
在应用的根目录下面分别建立子模块的目录:<WebRoot>/module1 <WebRoot>/module2。里面放置子模块自己的jsp,html和图片等资源。
这里需要注意,在配置web.xml时指定的"config/module1"就已经隐含的指定子模块的名字分别是module1,module2
所以,子模块的目录叫起名叫"module1"和"module2"
三个模块的struts配置:
默认模块(WEB-INF/struts-config.xml)
<action-mappings>
<action path="/welcome" forward="/index.jsp"></action>
</action-mappings>
子模块1(WEB-INF/module1/struts-config.xml)
<action-mappings>
<action path="/welcome" forward="/index.jsp"></action>
</action-mappings>
子模块2(WEB-INF/module2/struts-config.xml)
<action-mappings>
<action path="/welcome" forward="/index.jsp"></action>
</action-mappings>
三个模块之间的跳转:
默认模块(index.jsp)
<html:link module="/module1" action="/welcome">转到子模块1的主页面</html:link>
<html:link module="/module2" action="/welcome">转到子模块2的主页面</html:link>
子模块1(module1/index.jsp)
<html:link action="../welcome">转到主页面</html:link>
<html:link module="/module2" action="/welcome">转到子模块2的主页面</html:link>
子模块2(module2/index.jsp)
<html:link action="../welcome">转到主页面</html:link>
<html:link module="/module1" action="/welcome">转到子模块1的主页面</html:link>
模块之间的跳转还可以通过配置struts-config.xml中的<forward>元素,具体方法可以参照struts-documentation的说明。
在web.xml的servlet节点下,<load-on-startup>节点前添加:
<!-- 模块级配置 -->
<init-param>
<param-name>config/admin</param-name>
<param-value>/WEB-INF/config/struts-config-admin.xml</param-value>
</init-param>
在WEB-INF下添加config文件夹以及其中的struts-config-admin.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
</form-beans>
<action-mappings>
<!-- 产品管理配置 -->
<action path="/productservice"
type="com.newer.struts.action.productmanager.ProductManagerAction"
scope="request"
validate="false"
parameter="action">
<forward name="save" path="/productCreate.jsp"></forward>
</action>
</action-mappings>
</struts-config>