Struts 多模組

來源:互聯網
上載者:User

 

  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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.