当前位置:编程学习 > XML/UML >>

修改Struts2的struts.xml配置文件位置

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

 

\

如果需要修改struts.xml的位置,例如把struts.xml放到struts2文件夹下,结构如下图所示,该怎么办呢?

 

\

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

[html]  <!-- 配置struts2过滤器:StrutsPrepareAndExecuteFilter --> 
<filter> 
    <filter-name>struts2</filter-name> 
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>struts2</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

    <!-- 配置struts2过滤器:StrutsPrepareAndExecuteFilter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>为了弄清Stuts2是如何加载配置文件的,先查看Struts2的StrutsPrepareAndExecuteFilter类:

[java]  package org.apache.struts2.dispatcher.ng.filter; 
/**
 * Handles both the preparation and execution phases of the Struts dispatching process.  This filter is better to use
 * when you don't have another filter that needs access to action context information, such as Sitemesh.
 */ 
public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter { 
    protected PrepareOperations prepare; 
    protected ExecuteOperations execute; 
    protected List<Pattern> excludedPatterns = null; 
 
    public void init(FilterConfig filterConfig) throws ServletException { 
        InitOperations init = new InitOperations(); 
        try { 
            FilterHostConfig config = new FilterHostConfig(filterConfig); 
            init.initLogging(config); 
            Dispatcher dispatcher = init.initDispatcher(config); 
            init.initStaticContentLoader(config, dispatcher); 
 
            prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher); 
            execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher); 
            this.excludedPatterns = init.buildExcludedPatternsList(dispatcher); 
 
            postInit(dispatcher, filterConfig); 
        } finally { 
            init.cleanup(); 
        } 
 
    } 
 
    /**
     * Callback for post initialization
     */ 
    protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) { 
    } 
 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
 
        HttpServletRequest request = (HttpServletRequest) req; 
        HttpServletResponse response = (HttpServletResponse) res; 
 
        try { 
            prepare.setEncodingAndLocale(request, response); 
            prepare.createActionContext(request, response); 
            prepare.assignDispatcherToThread(); 
            if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { 
                chain.doFilter(request, response); 
            } else { 
                request = prepare.wrapRequest(request); 
                ActionMapping mapping = prepare.findActionMapping(request, response, true); 
                if (mapping == null) { 
                    boolean handled = execute.executeStaticResourceRequest(request, response); 
                    if (!handled) { 
                        chain.doFilter(request, response); 
                    } 
                } else { 
                    execute.executeAction(request, response, mapping); 
                } 
            } 
        } finally { 
            prepare.cleanupRequest(request); 
        } 
    } 
 
    public void destroy() { 
        prepare.cleanupDispatcher(); 
    } 

package org.apache.struts2.dispatcher.ng.filter;
/**
 * Handles both the preparation and execution phases of the Struts dispatching process.  This filter is better to use
 * when you don't have another filter that needs access to action context information, such

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,