Struts 多模块

来源:互联网 发布:603636南威软件 编辑:程序博客网 时间:2024/05/20 05:10

 

  1. web-inf下面的struts-config.xml是struts应用默认配置
  2. 对子模块的配置文件可以放在任意的位置,为了让Tomcat能够找到它们的位置,你需要在web.xml中配置
  3. <!-- module configurations -->
  4. <init-param>
  5. <param-name>config/module1</param-name>
  6. <param-value>/WEB-INF/module1/struts-config.xml</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>config/module2</param-name>
  10. <param-value>/WEB-INF/module2/struts-config.xml</param-value>
  11. </init-param>
  12. 在应用的根目录下面分别建立子模块的目录:<WebRoot>/module1 <WebRoot>/module2。里面放置子模块自己的jsp,html和图片等资源。
  13. 这里需要注意,在配置web.xml时指定的"config/module1"就已经隐含的指定子模块的名字分别是module1,module2
  14. 所以,子模块的目录叫起名叫"module1"和"module2"
  15. 三个模块的struts配置:
  16. 默认模块(WEB-INF/struts-config.xml)
  17. <action-mappings>
  18. <action path="/welcome" forward="/index.jsp"></action>
  19. </action-mappings>
  20. 子模块1(WEB-INF/module1/struts-config.xml)
  21. <action-mappings>
  22. <action path="/welcome" forward="/index.jsp"></action>
  23. </action-mappings>
  24. 子模块2(WEB-INF/module2/struts-config.xml)
  25. <action-mappings>
  26. <action path="/welcome" forward="/index.jsp"></action>
  27. </action-mappings>
  28. 三个模块之间的跳转:
  29. 默认模块(index.jsp)
  30. <html:link module="/module1" action="/welcome">转到子模块1的主页面</html:link>
  31. <html:link module="/module2" action="/welcome">转到子模块2的主页面</html:link>
  32. 子模块1(module1/index.jsp)
  33. <html:link action="../welcome">转到主页面</html:link>
  34. <html:link module="/module2" action="/welcome">转到子模块2的主页面</html:link>
  35. 子模块2(module2/index.jsp)
  36. <html:link action="../welcome">转到主页面</html:link>
  37. <html:link module="/module1" action="/welcome">转到子模块1的主页面</html:link>
  38. 模块之间的跳转还可以通过配置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.