- 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的说明。
struts中的module,实际上就类似于平日里开发web程序中的子目录
如
/- root
/music
/module
/...
例如上面的/music作为模块名
那么struts-config-music中的所有path默认即/music/xxx.do
一些教程中说要把jsp页面放入/web-inf中,这是个很好的方法,
一开始我也这么做,可惜,struts的action标签不支持contextRelative,只有forward支持
所以,如果要使用/xxx.do直接redirect或者forward到某个页面是行不通的。
因为他们的地址相对于/music/web-inf/xxx.jsp 这样就找不到了。
我的建议是,root下面的目录要和module一致。
例如
/
/moduleA
/moduleB
虽然安全性有些降低,但使用起来非常方便。
一般的,要写一个link,可以通过<html:link>来写
<html:link action="/module/action" >
他的好处是内置的支持module,不需要自己写.do,这就可以使得你的.do
任意的修改为别的而不影响程序运行。例如,.jspa,假装一下webwork
默认的<html:link>是相对module的。例如进入了/module/actionA指向的页面
在这个页面中,所有的link都被转换为相对于/module 如<html:link action="/actionB" >,实际上是/module/actionB
那么,如果要执行默认module的action怎么办?嘿嘿,其实很简单。我开始的时候绞尽脑汁,用了switchAction来解决/module/switch?prefix=&page=/xxx.do&.... 多麻烦
实际上,一个传统的办法可以有效解决。即<html:link action="../action" >
可以回到上一层的module中。我开始可真没想到。
link标签有page,href,action三种不同的链接方法。
其中,action默认的指向某个path,page指向一个jsp页面,也是相对于module的href可以写外部url.