struts2.X配置文件默認(rèn)存放路徑在/WEB-INF/classes目錄下,即將struts.xml放在src的目錄下。
但是為了方便管理,開(kāi)發(fā)人員把struts.xml放到其他位置,處理方法如下。
首先要明白struts2加載配置文件都是從自己的jar包和/WEB-INF/classes兩個(gè)默認(rèn)的位置加載的。
若修改struts2.x配置文件的存放位置,在web.xml配置過(guò)慮器時(shí),具體配置如下:
2 | < filter-name >struts2</ filter-name > |
3 | < filter-class >org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</ filter-class > |
5 | < param-name >config</ param-name > |
6 | < param-value >struts-default.xml,struts-plugin.xml,struts/struts.xml</ param-value > |
注意點(diǎn)1
若設(shè)置了<param-name>config</param-name>參數(shù),那struts-default.xml等原來(lái)struts2默認(rèn)加載的文件也要手動(dòng)指定,否則不會(huì)自動(dòng)加載。
注意點(diǎn)2
struts-plugin.xml也需要指定。因?yàn)樵趕truts2使用2.1.6版本時(shí):
若需要和spring集成的話,struts2-spring-plugin-2.1.6.jar中有struts-plugin.xml這個(gè)文件。
若struts2要支持json的話, json-plugin-0.34.jar中也有一個(gè)叫struts-plugin.xm的文件。
因此這個(gè)文件也是要加載的。
注意點(diǎn)3
采用相對(duì)/WEB-INF/classes的相對(duì)路徑。本例放在了/WEB-INF/classes/struts目錄下。當(dāng)然也可以寫(xiě)成classpath:struts/struts.xml
注意點(diǎn)4
若不在這里配置struts-default.xml,struts-plugin.xml,也可以在struts.xml文件中添加include標(biāo)簽將兩個(gè)文件包括進(jìn)去。
<include file="struts-default.xml" />和<include file="struts-plugin.xml" />
注意點(diǎn)5
使用<include file="..." />標(biāo)簽添加其他子配置文件時(shí),file屬性也要是一個(gè)相對(duì)/WEB-INF/classes的路徑。
若子配置文件路徑是/WEB-INF/classes/configs/struts/student/struts-config.xml的話,
file屬性值應(yīng)該寫(xiě)configs/struts/student/struts-config.xml。
若有多個(gè)子配置文件可以采用掃描的方式<include file="configs/struts/*/*.xml" />
可能遇到的問(wèn)題:
警告: Could not find action or result
There is no Action mapped for namespace / and action name hello. - [unknown location]
為什么指定了自己的struts.xml文件路徑依然訪問(wèn)不到呢?
原因依然在struts加載配置文件的方式,struts并不是獲取的配置文件相對(duì)應(yīng)用(項(xiàng)目)的路徑,而是相對(duì)src,對(duì)于web是相對(duì)/WEB-INF/classes文件夾的路徑,現(xiàn)在知道了最終的解決方案了?
對(duì)了,就是把web.xml中的[/WEB-INF/struts.xml]改成 [../struts.xml],即使用相對(duì)/WEB-INF/classes文件夾的路徑!
來(lái)自:http://blog.csdn.net/xiayuzheng/article/details/12319469