struts2.0 加載多個配置文件

解決了,給大家 看一下,多多指教
1.重寫FilterDispatcher 類的三個方法,我的struts-*.xml的路徑在WEB-INF/modules/struts文件夾下,JLTEnvironment類爲我的應用的配置路徑
public class JLTFilterDispatcher extends FilterDispatcher {
@Override
protected Dispatcher createDispatcher(FilterConfig filterConfig) {
Map <String, String> params = new HashMap <String, String>();
for (Enumeration e = filterConfig.getInitParameterNames(); e
.hasMoreElements();) {
String name = (String) e.nextElement();
String value = filterConfig.getInitParameter(name);
params.put(name, value);
}
// 加載modules下的struts配置文件
getStrutsConfig(params);
return new Dispatcher(filterConfig.getServletContext(), params);
}
      * 加載modules下的struts配置文件
private void getStrutsConfig(Map <String, String> m) {
String strutsPath = new String(
"struts-default.xml,struts-plugin.xml,struts.xml");
File f = new File(JLTEnvironment.getModulesHome()+"/struts");

if (f.getName().equals("struts")) {
File[] ff = f.listFiles();
if (ff != null && ff.length > 0) {
for (int i = 0; i < ff.length; i++) {
String fname = ff[i].getName();

if (fname.startsWith("struts-") && fname.endsWith(".xml")) {
strutsPath+=","+ff[i].getAbsolutePath();
}
}
}
}

m.put("config", strutsPath);

}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//獲得應用的路徑
        ServletContext ctx = filterConfig.getServletContext();
        String home = ctx.getRealPath("/");
        home = home.replace('//', '/');
        if (!home.endsWith("/")) {
            home = home + "/";
        }//初始化應用環境參數
JLTEnvironment.init(home, ctx);
super.init(filterConfig);
}

}
2.web.xml更改爲
        <filter>
<filter-name>struts2 </filter-name>
<filter-class>
com.jlt.core.JLTFilterDispatcher
</filter-class>
</filter>
3.這樣,WEB-INF/modules/struts下的所有以struts-開關的以xml結尾的xml文件都會被自動加載進去,不用去改其它配置了,呵呵

 

 

 

 

WEB-INF/struts/struts-sample.xml
配置如下
<filter>
        <filter-name>struts2 </filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class>
        <init-param>   
            <param-name>config </param-name>   
    <param-value>
       struts-default.xml
      ,struts-plugin.xml
      ,../struts/struts.xml
      ,../struts/struts-sample.xml
    </param-value>   
</init-param>
</filter> 
<filter-mapping>
        <filter-name>struts2 </filter-name>
        <url-pattern>/* </url-pattern>
    </filter-mapping>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章