您的位置:首页 > 编程语言 > Java开发

更改Struts的配置文件struts.xml 默认目录路径到WEB-INF

2012-07-20 14:28 375 查看
我的文件目录

-src

-WEB-INF

--classes

--lib

--struts-config-sale.xml

--struts-config-hr.xml

--struts.xml

--web.xml

在web.xml中加上

<filter>

<filter-name>struts</filter-name>

<filter-class>

org.apache.struts2.dispatcher.FilterDispatcher

</filter-class>

<init-param>

<param-name>config</param-name>

<param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>struts</filter-name>

<url-pattern>*.action</url-pattern>

</filter-mapping>

注意../struts.xml前面有两个点,表示上级目录,struts2默认查找的是/WEB-INF/classes下的配置文件,所以要返回上级目录去查找struts.xml

如果在struts.xm中的如果要包含其他struts的配置文件,也要更改目录

<struts>

...

<include file="../struts-config-sale.xml" />

<include file="../struts-config-hr.xml" />

</struts>

源代码:

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

}

}

}

所以上面的配置只是改变了读取配置文件的变量值,struts2始终还是从/WEB-INF/classes目录下开始搜索配置文件。这样配置就OK了,不需要修改其他的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: