精品推荐
阅读排行
· 查看svchost.exe进程· PRO/E 十种技巧
· [组图] 3ds Max 高级长篇人
· [组图] PRO/E的曲面设计
· 怎样学好PRO/E软件?
· 路由技术介绍
· Pro/ENGINEER 学习资
· [组图] Photoshop制作珠宝文
· xml的应用是什么?x
· [组图] flash人物绘画教程
| 作者:佚名 来源:www.pccode.net 整理 发布时间:2006-3-8 16:06:21 发布人:wongrs |
| 在struts1.1中使用应用模块简介 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正式版中已经支持多了配置文件] |
| [ ] [返回上一页] [打 印] [收 藏] |
上一篇文章:Struts的异常处理机制
下一篇文章:为什么要升级到struts1.1 |
