- 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.