struts2中如何自动加载xml文件(不用修改配置文件)
1.这个是我在网上找的,作个比较(需要修改web.xml)? 在web.xml配置初始参数config
<filter> <filter-name>struts2 </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,/WEB-INF/struts.xml </param-value> </init-param></filter><filter-mapping> <filter-name>struts2 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
?以上配置方式已通过测试,成功!,在些方法要经常改动web.xml,多人合作开发的话,很容易冲突
2.在struts.xml中使用include标签(需要修改公共的xml文件)
http://www.qingsoft.net/bbs/html/article/1094.jhtml
?
3.以上方法在多人开发时比较麻烦,以下也就是我自己的方法喽
1.重写FilterDispatcher 类的三个方法,我的struts-*.xml的路径在WEB-INF/modules/struts文件夹下
,JLTEnvironment类为我的应用的配置路径
public class JLTFilterDispatcher extends FilterDispatcher { @Override protected Dispatcher createDispatcher(FilterConfig rConfig) { Map <String, String> params = new HashMap <String, String>(); for (Enumeration e = filterConfig.getInitParameterNames); e .hasMoreElements();) { String name = (String) e.nextElement(); String value = filterConfig.getInitParameter(name); params.put(name, value); } // 加载modules下的struts配置文件 getStrutsConfig(params); return new Dispatcher(filterConfig.getServletContext(), params); }// 加载modules下的struts配置文件 private void getStrutsConfig(Map <String, String> m) { String strutsPath = new String( "struts-default.xml,struts-plugin.xml,struts.xml"); File f = new File(JLTEnvironment.getModulesHome()+"/struts"); if (f.getName().equals("struts")) { File[] ff = f.listFiles(); if (ff != null && ff.length > 0) { for (int i = 0; i < ff.length; i++) { String fname = ff[i].getName(); if (fname.startsWith("struts-") && fname.endsWith(".xml")) { strutsPath+=","+ff[i].getAbsolutePath(); } } } m.put("config", strutsPath);}@Override public void init(FilterConfig filterConfig) throws ServletException { //获得应用的路径 ServletContext ctx = filterConfig.getServletContext(); String home = ctx.getRealPath("/"); home = home.replace('\\', '/'); if (!home.endsWith("/")) { home = home + "/"; } //初始化应用环境参数 JLTEnvironment.init(home, ctx); super.init(filterConfig); } ?
2.web.xml更改为
?
<filter> <filter-name>struts2 </filter-name> <filter-class> com.jlt.core.JLTFilterDispatcher </filter-class></filter>
?
3.这样,WEB-INF/modules/struts下的所有以struts-开头的以xml结尾的xml文件都会被自动加载进去,
? 不用去改其它配置了,呵呵?
??再添加struts配置文件的话,只要放在WEB-INF/modules/struts目录下,会自动被加载
?
1 楼 myyate 2009-02-25 struts2 1.2.*以上支持include正则配置,你这样又是轮子了 2 楼 atian25 2009-02-25 <init-param><param-name>config </param-name>
<param-value>struts-default.xml,struts-plugin.xml,/WEB-INF/struts.xml </param-value>
</init-param>
这个我这边测试不通过,需要把/WEB-INF/struts.xml修改为../struts.xml才能通过 3 楼 wangneng_001 2009-02-25 myyate
那个include正则杂写的?? 4 楼 lisg 2009-02-26 struts.properties
struts.configuration.xml.reload = true 5 楼 wangneng_001 2009-02-26 我看了官方struts2.1.6的文档
没见着说include可以正则匹配呀 6 楼 myyate 2009-02-27 wangneng_001 写道我看了官方struts2.1.6的文档
没见着说include可以正则匹配呀
我是看struts2.1.6源码的时候发现的。
可以这样写:<include file="actions_*.xml"/>
参见XmlConfigurationProvider的源代码 7 楼 wangneng_001 2009-02-27 myyate 写道wangneng_001 写道我看了官方struts2.1.6的文档
没见着说include可以正则匹配呀
我是看struts2.1.6源码的时候发现的。
可以这样写:<include file="actions_*.xml"/>
参见XmlConfigurationProvider的源代码
刚才试了一下,好像只有在classpath下的xml才能匹配到。。。而且文档上居然没提到说可以匹配 靠
8 楼 smilebug 2009-03-19 wangneng_001 写道myyate 写道wangneng_001 写道我看了官方struts2.1.6的文档
没见着说include可以正则匹配呀
我是看struts2.1.6源码的时候发现的。
可以这样写:<include file="actions_*.xml"/>
参见XmlConfigurationProvider的源代码
刚才试了一下,好像只有在classpath下的xml才能匹配到。。。而且文档上居然没提到说可以匹配 靠
如果不在classpath下面,在前面加/WEB-INF/actions_*.xml路径貌似也可以的 9 楼 Dead_knight 2009-04-18 smilebug 写道wangneng_001 写道myyate 写道wangneng_001 写道我看了官方struts2.1.6的文档
没见着说include可以正则匹配呀
我是看struts2.1.6源码的时候发现的。
可以这样写:<include file="actions_*.xml"/>
参见XmlConfigurationProvider的源代码
刚才试了一下,好像只有在classpath下的xml才能匹配到。。。而且文档上居然没提到说可以匹配 靠
如果不在classpath下面,在前面加/WEB-INF/actions_*.xml路径貌似也可以的
不过大型项目每个模块都有/模块名称/WEB-INF/classes/struts.xml的,觉得还是自己重写的好
不知道struts2是否支持配置文件修改过后自动装载的机制,如果没有还要定时去装载修改后的struts.xml文件
10 楼 inbow 2009-05-13 楼主把JLTEnvironment贴出来吧