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

来源:转载

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

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

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

<!-- 配置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类:

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();

}

}

可以看到,有个public void init(FilterConfig filterConfig) throws ServletException { ... } 方法,很明显,这个方法在启动时会被调用,然后加载classes目录下的struts.xml配置文件。配置文件参数名称为filterConfig。因此,我们可以指定Struts2 Filter 的参数,这和指定Servlet参数相同,配置<init-param>即可。配置如下:

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

<init-param>

<param-name>filterConfig</param-name>

<param-value>classpath:struts2/struts.xml</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

这样配置后,就可以正常加载指定的Struts配置文件了。

 

分享给朋友:
您可能感兴趣的文章:
随机阅读: