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

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

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

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

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

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

为了弄清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 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参数相同,配置即可。配置如下:

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

filterConfig

classpath:struts2/struts.xml

struts2

/*

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

相关推荐

汐玥名字的含义寓意 汐玥名字的意思五行
中国的365体育投注

汐玥名字的含义寓意 汐玥名字的意思五行

06-27 👁️ 5614
英雄联盟手游小鱼人主什么副什么
中国的365体育投注

英雄联盟手游小鱼人主什么副什么

06-28 👁️ 4014
天涯明月刀手游太白和真武哪个好 二者对比分析介绍
网上注册送365的平台

天涯明月刀手游太白和真武哪个好 二者对比分析介绍

06-27 👁️ 2121