struts2中如何自动加载xml文件(不用修改配置文件)(转)
来源:互联网 发布:焊接机械手编程 编辑:程序博客网 时间:2024/06/07 18:29
主题:struts2中如何自动加载xml文件(不用修改配置文件)
转自:http://www.iteye.com/topic/338419
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>
<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);
- }
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>
<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目录下,会自动被加载
- struts2中如何自动加载xml文件(不用修改配置文件)(转)
- struts2中如何自动加载xml文件(不用修改配置文件)
- Struts2中如何加载多个struts.xml配置文件
- struts2如何加载配置文件(总结)
- Configuration -Properties 文件 修改配置文件自动加载
- Spring Xml配置文件自动加载properties文件
- 监听文件修改,自动加载xml文件。
- struts2中的配置文件的<constant>标签(struts.xml自动加载)
- applicationContext.xml文件中怎么配置struts2和spring配置文件的加载路径
- SqlMapConfig.xml --- 在spring配置文件中自动加载的mybatis文件
- SqlMapConfig.xml---在spring配置文件中自动加载的mybatis文件
- Spring 配置文件xxxx.xml文件中scheme约束exlipse自动加载不了的解决
- struts2中struts.properties配置文件详解(及在struts.xml文件中的配置)
- 如何实现在myeclipse和eclipse中,修改代码后实现自动加载,不用频繁的手动启动tomcat
- apache commons configuration自动加载修改的配置文件不用重启服务器
- struts2.0中如何将struts.xml文件分成多个配置文件
- MyEclipse中配置Struts2配置文件XML自动提示
- MyEclipse中配置Struts2配置文件XML自动提示
- PHP命令执行PHP脚本,结束之前,内存会回收吗?
- Sql Server 分页查询
- 黑马程序员----JAVA基础反射与注解
- Create custom Task List and Forms in SharePoint 2010 with Visual Studio 2012
- android proguard 原理
- struts2中如何自动加载xml文件(不用修改配置文件)(转)
- RDMA技术
- 写一个函数,检查字符是否是整数,如果是,返回其整数值
- VMware vSphere五种RDMA应用模型
- 备受开发者青睐的13款热门开源项目
- Jqgrid安装(第一个Grid-xml)
- 黑马程序员——集合类中关于HashSet类和TreeSet类的理解
- 使用.net调用Solr
- UVA 10585 - Center of symmetry