我是靠谱客的博主 默默篮球,这篇文章主要介绍Struts2更改配置文件默认路径,现在分享给大家,希望可以做个参考。

Struts2默认的配置文件路径在classpath的根目录下,但是在实际的开发中,放在这个位置并不是最好的选择,那么怎么修改呢?

1、要修改配置文件的路径,首先要明白Struts2的配置文件是怎么加载的,这就要讲到Struts2的默认拦截器StrutsPrepareAndExecuteFilter了;
在StrutsPrepareAndExecuteFilter的init()方法中

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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(); } }

其中 Dispatcher dispatcher = init.initDispatcher(config);的initDispatcher方法点开后

复制代码
1
2
3
4
5
public Dispatcher initDispatcher( HostConfig filterConfig ) { Dispatcher dispatcher = createDispatcher(filterConfig); dispatcher.init(); return dispatcher; }

看其中的init()方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public void init() { if (configurationManager == null) { configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME); } try { init_DefaultProperties(); // [1] init_TraditionalXmlConfigurations(); // [2] init_LegacyStrutsProperties(); // [3] init_CustomConfigurationProviders(); // [5] init_FilterInitParameters() ; // [6] init_AliasStandardObjects() ; // [7] Container container = init_PreloadConfiguration(); container.inject(this); init_CheckConfigurationReloading(container); init_CheckWebLogicWorkaround(container); if (!dispatcherListeners.isEmpty()) { for (DispatcherListener l : dispatcherListeners) { l.dispatcherInitialized(this); } } } catch (Exception ex) { if (LOG.isErrorEnabled()) LOG.error("Dispatcher initialization failed", ex); throw new StrutsException(ex); } }

重点关注init_TraditionalXmlConfigurations()方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml"; private void init_TraditionalXmlConfigurations() { String configPaths = initParams.get("config"); if (configPaths == null) { configPaths = DEFAULT_CONFIGURATION_PATHS; } String[] files = configPaths.split("\s*[,]\s*"); for (String file : files) { if (file.endsWith(".xml")) { if ("xwork.xml".equals(file)) { configurationManager.addConfigurationProvider(new XmlConfigurationProvider(file, false)); } else { configurationManager.addConfigurationProvider(new StrutsXmlConfigurationProvider(file, false, servletContext)); } } else { throw new IllegalArgumentException("Invalid configuration file name"); } } }

到这儿就知道了若要修改Struts的配置文件的路径,在web.xml文件中的参数名称了为“config”,因为
String configPaths = initParams.get(“config”);

若为空,默认加载classpath路径下的”struts-default.xml,struts-plugin.xml,struts.xml”;

修改后的参数为“config” 那值应该怎么写呢?

*首先要明白classpath的路径为WEB-INF下的classes文件夹,所以默认的配置文件的路径就在这里面,要修改其配置文件路径,开发者只修改struts.xml的路径,所以首先要找到classes的目录相对于你现在的配置文件的路径写法 ;
其次,修改了struts.xml的路径,但是struts-default.xml,struts-plugin.xml,的路径并没有发生变化,所以要继续加载,不加载会“死人”的。*

本人的struts.xml路径在WEB-INF下面的config下,所以在web.xml里面的配置最终变为如下(实验后完全可以):

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 配置Struts2的过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>config</param-name> **<param-value>struts-default.xml,struts-plugin.xml,../config/struts.xml</param-value>** </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

最后

以上就是默默篮球最近收集整理的关于Struts2更改配置文件默认路径的全部内容,更多相关Struts2更改配置文件默认路径内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部