java

位置:IT落伍者 >> java >> 浏览文章

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


发布日期:2020年02月17日
 
修改Struts2的struts.xml配置文件位置

默认情况下Struts的配置文件名称为strutsxml且该文件放在src根目录下如下图所示

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

Struts在webxml中的一般配置如下

[html]

struts

orgapachestrutsdispatcherngfilterStrutsPrepareAndExecuteFilter

struts

/*

struts

orgapachestrutsdispatcherngfilterStrutsPrepareAndExecuteFilter

struts

/*

为了弄清Stuts是如何加载配置文件的先查看Struts的StrutsPrepareAndExecuteFilter类

[java] package orgapachestrutsdispatcherngfilter;

/**

* Handles both the preparation and execution phases of the Struts dispatching process This filter is better to use

* when you dont 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);

initinitLogging(config);

Dispatcher dispatcher = initinitDispatcher(config);

initinitStaticContentLoader(config dispatcher);

prepare = new PrepareOperations(filterConfiggetServletContext() dispatcher);

execute = new ExecuteOperations(filterConfiggetServletContext() dispatcher);

thisexcludedPatterns = initbuildExcludedPatternsList(dispatcher);

postInit(dispatcher filterConfig);

} finally {

initcleanup();

}

}

/**

* 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 {

preparesetEncodingAndLocale(request response);

preparecreateActionContext(request response);

prepareassignDispatcherToThread();

if ( excludedPatterns != null && prepareisUrlExcluded(request excludedPatterns)) {

chaindoFilter(request response);

} else {

request = preparewrapRequest(request);

ActionMapping mapping = preparefindActionMapping(request response true);

if (mapping == null) {

boolean handled = executeexecuteStaticResourceRequest(request response);

if (!handled) {

chaindoFilter(request response);

}

} else {

executeexecuteAction(request response mapping);

}

}

} finally {

preparecleanupRequest(request);

}

}

public void destroy() {

preparecleanupDispatcher();

}

}

package orgapachestrutsdispatcherngfilter;

/**

* Handles both the preparation and execution phases of the Struts dispatching process This filter is better to use

* when you dont 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);

initinitLogging(config);

Dispatcher dispatcher = initinitDispatcher(config);

initinitStaticContentLoader(config dispatcher);

prepare = new PrepareOperations(filterConfiggetServletContext() dispatcher);

execute = new ExecuteOperations(filterConfiggetServletContext() dispatcher);

thisexcludedPatterns = initbuildExcludedPatternsList(dispatcher);

postInit(dispatcher filterConfig);

} finally {

initcleanup();

}

}

/**

* 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 {

preparesetEncodingAndLocale(request response);

preparecreateActionContext(request response);

prepareassignDispatcherToThread();

if ( excludedPatterns != null && prepareisUrlExcluded(request excludedPatterns)) {

chaindoFilter(request response);

} else {

request = preparewrapRequest(request);

ActionMapping mapping = preparefindActionMapping(request response true);

if (mapping == null) {

boolean handled = executeexecuteStaticResourceRequest(request response);

if (!handled) {

chaindoFilter(request response);

}

} else {

executeexecuteAction(request response mapping);

}

}

} finally {

preparecleanupRequest(request);

}

}

public void destroy() {

preparecleanupDispatcher();

}

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

[html]

struts

orgapachestrutsdispatcherngfilterStrutsPrepareAndExecuteFilter

filterConfig

classpath:struts/strutsxml

struts

/*

struts

orgapachestrutsdispatcherngfilterStrutsPrepareAndExecuteFilter

filterConfig

classpath:struts/strutsxml

struts

/*

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

               

上一篇:基于Spring实现seam形式的事件机制

下一篇:Eclipse3.3下VSS的安装和使用步骤