struts2中如何自動加載xml文件(不用修改配置文件)(轉)

主題:struts2中如何自動加載xml文件(不用修改配置文件)

轉自:http://www.iteye.com/topic/338419

 

config

Xml代碼 複製代碼 收藏代碼
  1. <filter>  
  2.    <filter-name>struts2 </filter-name>  
  3.    <filter-class>  
  4.        org.apache.struts2.dispatcher.FilterDispatcher   
  5.    </filter-class>  
  6.    <init-param>    
  7.       <param-name>config </param-name>    
  8.       <param-value>struts-default.xml,struts-plugin.xml,/WEB-INF/struts.xml </param-value>    
  9.    </init-param>  
  10. </filter>  
  11.   
  12. <filter-mapping>    
  13.    <filter-name>struts2 </filter-name>  
  14.    <url-pattern>/* </url-pattern>    
  15. </filter-mapping>  

 以上配置方式已通過測試,成功!,在些方法要經常改動web.xml,多人合作開發的話,很容易衝突

2.在struts.xml中使用include標籤(需要修改公共的xml文件)
http://www.qingsoft.net/bbs/html/article/1094.jhtml

 

3.以上方法在多人開發時比較麻煩,以下也就是我自己的方法嘍
1.重寫FilterDispatcher 類的三個方法,我的struts-*.xml的路徑在WEB-INF/modules/struts文件夾下

,JLTEnvironment類爲我的應用的配置路徑

Java代碼 複製代碼 收藏代碼
  1. public class JLTFilterDispatcher extends FilterDispatcher {    
  2. @Override  
  3.  protected Dispatcher createDispatcher(FilterConfig rConfig)         {    
  4.    Map <String, String> params = new HashMap <String, String>();   
  5.   
  6.    for (Enumeration e =  filterConfig.getInitParameterNames);   
  7.           e .hasMoreElements();)    
  8.   {    
  9.         String name = (String) e.nextElement();    
  10.         String value = filterConfig.getInitParameter(name);   
  11.         params.put(name, value);    
  12.   }    
  13.   // 加載modules下的struts配置文件    
  14.   getStrutsConfig(params);    
  15.    return  
  16.    new Dispatcher(filterConfig.getServletContext(), params);    
  17. }   
  18.   
  19. // 加載modules下的struts配置文件    
  20. private void getStrutsConfig(Map <String, String> m) {    
  21.    String strutsPath =   
  22.    new String( "struts-default.xml,struts-plugin.xml,struts.xml");    
  23.    File f = new File(JLTEnvironment.getModulesHome()+"/struts");    
  24.    if (f.getName().equals("struts")) {    
  25.          File[] ff = f.listFiles(); if (ff != null && ff.length > 0) {    
  26.          for (int i = 0; i < ff.length; i++) {    
  27.                String fname = ff[i].getName();    
  28.                if (fname.startsWith("struts-")    
  29.                    && fname.endsWith(".xml")) {   
  30.                      strutsPath+=","+ff[i].getAbsolutePath();   
  31.                 }    
  32.         }    
  33.     }   
  34.   
  35.   m.put("config", strutsPath);   
  36. }   
  37.   
  38. @Override    
  39. public void init(FilterConfig filterConfig) throws ServletException {    
  40.    //獲得應用的路徑    
  41.   ServletContext ctx = filterConfig.getServletContext();    
  42.    String home = ctx.getRealPath("/");   
  43.    home = home.replace('\\', '/');    
  44.    if (!home.endsWith("/")) {   
  45.          home = home + "/";   
  46.    }   
  47.   //初始化應用環境參數    
  48.   JLTEnvironment.init(home, ctx); super.init(filterConfig);    
  49. }   
public class JLTFilterDispatcher extends FilterDispatcher { 
@Override
 protected Dispatcher createDispatcher(FilterConfig rConfig)         { 
   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更改爲

 

Xml代碼 複製代碼 收藏代碼
  1. <filter>    
  2.    <filter-name>struts2 </filter-name>  
  3.    <filter-class>  
  4.          com.jlt.core.JLTFilterDispatcher    
  5.    </filter-class>  
  6. </filter>  

 

3.這樣,WEB-INF/modules/struts下的所有以struts-開頭的以xml結尾的xml文件都會被自動加載進去,

  不用去改其它配置了,呵呵 

  再添加struts配置文件的話,只要放在WEB-INF/modules/struts目錄下,會自動被加載

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