zoukankan      html  css  js  c++  java
  • 修改Struts2的struts.xml配置文件位置

    默认情况下,Struts2的配置文件名称为struts.xml,且该文件放在src根目录下。如下图所示:

    如果需要修改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. public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {  
    7.     protected PrepareOperations prepare;  
    8.     protected ExecuteOperations execute;  
    9.     protected List<Pattern> excludedPatterns = null;  
    10.   
    11.     public void init(FilterConfig filterConfig) throws ServletException {  
    12.         InitOperations init = new InitOperations();  
    13.         try {  
    14.             FilterHostConfig config = new FilterHostConfig(filterConfig);  
    15.             init.initLogging(config);  
    16.             Dispatcher dispatcher = init.initDispatcher(config);  
    17.             init.initStaticContentLoader(config, dispatcher);  
    18.   
    19.             prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);  
    20.             execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);  
    21.             this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);  
    22.   
    23.             postInit(dispatcher, filterConfig);  
    24.         } finally {  
    25.             init.cleanup();  
    26.         }  
    27.   
    28.     }  
    29.   
    30.     /** 
    31.      * Callback for post initialization 
    32.      */  
    33.     protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) {  
    34.     }  
    35.   
    36.     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {  
    37.   
    38.         HttpServletRequest request = (HttpServletRequest) req;  
    39.         HttpServletResponse response = (HttpServletResponse) res;  
    40.   
    41.         try {  
    42.             prepare.setEncodingAndLocale(request, response);  
    43.             prepare.createActionContext(request, response);  
    44.             prepare.assignDispatcherToThread();  
    45.             if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {  
    46.                 chain.doFilter(request, response);  
    47.             } else {  
    48.                 request = prepare.wrapRequest(request);  
    49.                 ActionMapping mapping = prepare.findActionMapping(request, response, true);  
    50.                 if (mapping == null) {  
    51.                     boolean handled = execute.executeStaticResourceRequest(request, response);  
    52.                     if (!handled) {  
    53.                         chain.doFilter(request, response);  
    54.                     }  
    55.                 } else {  
    56.                     execute.executeAction(request, response, mapping);  
    57.                 }  
    58.             }  
    59.         } finally {  
    60.             prepare.cleanupRequest(request);  
    61.         }  
    62.     }  
    63.   
    64.     public void destroy() {  
    65.         prepare.cleanupDispatcher();  
    66.     }  
    67. }  

    可以看到,有个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配置文件了。

  • 相关阅读:
    UIActivityIndicatorView的详细使用
    iOS开发多线程篇—GCD的常见用法
    UIScrollView的属性总结
    关于UIView的autoresizingMask属性的研究
    Robot FrameWork 教程链接
    数据恢复基础知识
    数据恢复基础知识
    selenium webdriver 学习笔记(三)
    selenium webdriver 学习笔记(二)
    selenium webdriver 学习笔记(一)
  • 原文地址:https://www.cnblogs.com/surge/p/3581954.html
Copyright © 2011-2022 走看看