今天找了一下struts2设置加载非默认路径的struts.xml文件的方法 但很多都是一笔代过, 后来自己弄清楚了,在这里和初学的同学一起学习一下, 还请老鸟们多指教 首先我们要明白struts2加载配置文件都是从它自己的jar包 和\WEB-INF\classes两个默认的位置加载的, 如果你想改变可以在web项目的web.xml配置过虑器的时候指定 如(配置过虑器的部分代码):
<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,../STRUTS-XML/struts.xml</param-value> </init-param> </filter> 我把struts.xml放在 WEB-INF\STRUTS-XML\struts.xml window2003下试过成功,要注意的问题:1.我用".."后退退到WEB-INF目录(在很多资料中就没有说明这一点,所以有些人想放到WEB-INF目录都出错) 为不了解其它的操作系统,如果你要把struts.xml放在这些位置时要考虑这一点(兼容性)2.要注意的是,如果你设置了<param-name>config</param-name>参数, 那么struts-default.xml等原来struts2默认加载的文件你也要指定,否则它好像不会自动加载,会出错 也可以加载到struts.xml中(<include file="struts-default.xml"></include>) (另外 struts-default.xml,struts-plugin.xml 是不用特殊指定的 struts.xml放到根目录下就可以 在struts.xml 里include 其他的struts.xml 而struts.xml 主要是进行一些配置的(比如字符类型等))3.而且使用<include file="../STRUTS-XML/user.xml"/>标签时也要系一个都要指定相对的路径和struts.xml指定一样还有一种不用这样配置的方法就是使用<include file="/STRUTS-XML/user.xml"/>也就是struts.xml放在默认的位置,只是其它的配置放到其它的地方如:
我就是把user.xml文件放到了\WEB-INF\classes\STRUTS-XML目录下 这样也可以达到统一管理配置文件,也不用配置<param-name>config</param-name>参数
或者
在struts2的配置路径中默认是在/WEB-INF/classes下 这样可能有一些不方便,比如我们的所有配置文件在/WEB-INF/conf下面,其实配置比较简单就是如下: <filter> 本来认为成功,结果启动不起来报错,原因是因为更改了config后会把默认的和现有的不能存在继承关系只好包含进来就可以修改struts.xml文件 <struts> <include file="struts-default.xml" />..... </struts> 填上这个就可以了一切OK。
附加资料 使用<include>标签重用配置文件 结构更清晰,更容易维护配置信息。 <?xml version="1.0" encoding="UTF-8" ?>
则struts.xml引用newstruts.xml文件的代码如下:
|
Struts2设置加载非默认路径的struts.xml文件的方法
最新推荐文章于 2018-12-26 15:24:41 发布
2011年06月09日 星期四 上午 11:01