1. 程式人生 > >修改Struts2的struts.xml配置檔案位置和名稱-重點是init-param引數用來切換載入的路徑

修改Struts2的struts.xml配置檔案位置和名稱-重點是init-param引數用來切換載入的路徑

預設情況下,Struts2的配置檔名稱為struts.xml,且該檔案放在src根目錄下。專案載入時會自動載入該struts.xml,如下圖所示:

如果需要修改struts.xml的位置,例如把struts.xml放到struts2資料夾下,結構如下圖所示,該怎麼辦呢?

Struts2在web.xml中的一般配置如下:

  1. <!-- 配置struts2過濾器:StrutsPrepareAndExecuteFilter -->
  2. <filter>
  3.     <filter-name>struts2</filter-name>
  4.     <
    filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7.     <filter-name>struts2</filter-name>
  8.     <url-pattern>/*</url-pattern>
  9. </filter-mapping>

為了弄清Stuts2是如何載入配置檔案的,先檢視Struts2的StrutsPrepareAndExecuteFilter類:

  1. package org.apache.struts2.dispatcher.ng.filter;  
  2. /** 
  3.  * Handles both the preparation and execution phases of the Struts dispatching process.  This filter is better to use 
  4.  * when you don't have another filter that needs access to action context information, such as Sitemesh. 
  5.  */
  6. publicclass StrutsPrepareAndExecuteFilter 
    implements StrutsStatics, Filter {  
  7.     protected PrepareOperations prepare;  
  8.     protected ExecuteOperations execute;  
  9.     protected List<Pattern> excludedPatterns = null;  
  10.     publicvoid init(FilterConfig filterConfig) throws ServletException {  
  11.         InitOperations init = new InitOperations();  
  12.         try {  
  13.             FilterHostConfig config = new FilterHostConfig(filterConfig);  
  14.             init.initLogging(config);  
  15.             Dispatcher dispatcher = init.initDispatcher(config);  
  16.             init.initStaticContentLoader(config, dispatcher);  
  17.             prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);  
  18.             execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);  
  19.             this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);  
  20.             postInit(dispatcher, filterConfig);  
  21.         } finally {  
  22.             init.cleanup();  
  23.         }  
  24.     }  
  25.     /** 
  26.      * Callback for post initialization 
  27.      */
  28.     protectedvoid postInit(Dispatcher dispatcher, FilterConfig filterConfig) {  
  29.     }  
  30.     publicvoid doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {  
  31.         HttpServletRequest request = (HttpServletRequest) req;  
  32.         HttpServletResponse response = (HttpServletResponse) res;  
  33.         try {  
  34.             prepare.setEncodingAndLocale(request, response);  
  35.             prepare.createActionContext(request, response);  
  36.             prepare.assignDispatcherToThread();  
  37.             if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {  
  38.                 chain.doFilter(request, response);  
  39.             } else {  
  40.                 request = prepare.wrapRequest(request);  
  41.                 ActionMapping mapping = prepare.findActionMapping(request, response, true);  
  42.                 if (mapping == null) {  
  43.                     boolean handled = execute.executeStaticResourceRequest(request, response);  
  44.                     if (!handled) {  
  45.                         chain.doFilter(request, response);  
  46.                     }  
  47.                 } else {  
  48.                     execute.executeAction(request, response, mapping);  
  49.                 }  
  50.             }  
  51.         } finally {  
  52.             prepare.cleanupRequest(request);  
  53.         }  
  54.     }  
  55.     publicvoid destroy() {  
  56.         prepare.cleanupDispatcher();  
  57.     }  
  58. }  

可以看到,有個public void init(FilterConfig filterConfig) throws ServletException { ... } 方法,很明顯,這個方法在啟動時會被呼叫,然後載入classes目錄下的struts.xml配置檔案。配置檔案引數名稱為filterConfig。因此,我們可以指定Struts2 Filter 的引數,這和指定Servlet引數相同,配置<init-param>即可。配置如下:

  1. <filter>
  2.     <filter-name>struts2</filter-name>
  3.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  4.     <init-param>
  5.         <param-name>filterConfig</param-name>
  6.         <param-value>classpath:struts2/struts.xml</param-value>
  7.     </init-param>
  8. </filter>
  9. <filter-mapping>
  10.     <filter-name>struts2</filter-name>
  11.     <url-pattern>/*</url-pattern>
  12. </filter-mapping>


這樣配置後,就可以正常載入指定的Struts配置檔案了。