struts 1.1的一个新特性是应用模块的概念。应用模块允许将单个struts应用划分成几个模块,每个模块有自己的struts配置文件, jsp页面,action等等。这个新特性是为了解决大中型的开发队伍抱怨最多的一个问题,即为了更好的支持并行开发允许多个配置 文件而不是单个配置文件。
在用struts进行大型项目开发中,使用这种新特性将是非常好的选择。
下面我以一个非常简单的例子来展示这一功能。
通常在单模块进行开发时,文件的最一般的目录结构如下:
test/
…./web-inf/
………../classes
………../lib
………../*.tld
………../struts-config.xml
……….. /web.xml
……….. /validator-rules.xml
……….. /validation.xml
……….. …
…./js
…./css
…./main1.jsp (本例使用的一个简单的jsp页面)
…./*.jsp
… ..
如果我们增加一个单独的模块如:test1,则文件的最一般的目录结构如下:
test/
…./test1 ****
………/main11.jsp (本例使用的一个简单的jsp页面)
………/*.jsp ****
…./web-inf/
………../classes
………../lib
………../*.tld
………../struts-config.xml
……….. /struts-test1-config.xml *****
……….. /web.xml
……….. /validator-rules.xml
……….. /validation.xml
……….. …
…/js
… /css
…/main1.jsp (本例使用的一个简单的jsp页面)
…/*.jsp
… ..
[旁边带有“****”的为新增的一些文件或目录]
以下为变更的几个xml配置文件:
1.struts-config.xml
在原来的action-mapping 增加switchaction 如:
….
….
<action-mappings>
<!– edit user registration –>
<action path="/login"
type="com.ifreeway.rms.v12.action.loginaction"
scope="request">
<forward name="error" path="/main1.jsp"/>
</action>
<action path="/login1"
type="com.ifreeway.rms.v12.action.loginaction1"
scope="request">
<forward name="error" path="/main1.jsp"/>
</action>
<action path="/switchdo" type="org.apache.struts.actions.switchaction"/> ****
</action-mappings>
….
….
[旁边带有“****”的为新特性中增加的配置]
2.struts-test1-config.xml
添加输入这个模块的actionmapping 如下:
….
….
<action-mappings>
<!– edit user registration –>
<action path="/login3"
type="com.ifreeway.rms.v12.action.loginaction2"
scope="request">
<forward name="error" path="/main11.jsp"/>
</action>
</action-mappings>
….
….
3.web.xml
….
….
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.actionservlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/web-inf/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>config/test1</param-name> *****
<param-value>/web-inf/struts-test1-config.xml</param-value> *****
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
….
….
[旁边带有“****”的为新特性中增加的配置]
大家可以看到在struts-test1-config.xml中增加了下面代码:
<action path="/switchdo" type="org.apache.struts.actions.switchaction"/>
因为在struts1.1中模块之间的导航要使用到org.apache.struts.actions.switchaction,如果你查看struts1.1的源代码可以发现
org.apache.struts.actions.switchaction需要两个参数:prefix 和 page 下面对这个参数逐一说明:
这两个参数均要以“/”开头
prefix 简单一点就是新增模块的名字,本例为:test1
page 就是在新的strutsconfig配置文件中要使用的actionmapping的名字 就本例为:login3.do
注意:如果是要使用却省模块的调用的话 prefix=空字符串
好了下面给出模块间转换的方法
启动tomcat
http://localhost:8080/test/switchdo.do?prefix=/test1&page=/login3.do
则调用struts-test1-config.xml下的/login3.do
http://localhost:8080/test/switchdo.do?prefix=&page=/login1.do
则调用struts-config.xml下的/login1.do
http://localhost:8080/test/switchdo.do?prefix=&page=/login.do
则调用struts-config.xml下的/login.do
总的来说模块之间的转换要用到org.apache.struts.actions.switchaction,而它需要传递两个参数(一般出错的地方极有可能出在参数
的错误上,请注意了)
[注:在struts1.1正式版中已经支持多了配置文件]
