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目录下,会自动被加载