struts2配置多个文件

在struts2中配置多个配置文件的方法很多,常见的几种为:

 

 

1.由于struts2默认会加载class下的中的struts.xml文件,所以可以采用了include方法将其他的配置文件包含进来,其中include包含可以支持正则表达式。

<include file="struts/struts-*.xml"></include>

 

2.第二种方法采用的是才web.xml中配置struts配置文件,具体如下:

 

<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,/WEB-INF/struts.xml,/WEB-INF/strtus/struts-conf/struts-*.xml</param-value>
     </init-param>  
 </filter>

 

3.第三种方法可以继承StrutsPrepareAndExecuteFilter重写init方法.

1.public class StrutsInitPrepareAndExecuteFilter extends
  StrutsPrepareAndExecuteFilter {

 public void init(FilterConfig filterConfig) throws ServletException {
  StrutsInitOperations init = new StrutsInitOperations();
  FilterHostConfig config = new FilterHostConfig(filterConfig);
  init.initLogging(config);
  Dispatcher dispatcher = init.initDispatcher(config);
  init.initStaticContentLoader(config, dispatcher);
  prepare = new PrepareOperations(filterConfig.getServletContext(),
    dispatcher);
  execute = new ExecuteOperations(filterConfig.getServletContext(),
    dispatcher);
  excludedPatterns = init.buildExcludedPatternsList(dispatcher);
  postInit(dispatcher, filterConfig);
  init.cleanup();
 }

}

 

2.StrutsInitOperations继承InitOperations类中的createDispatcher方法

 

public Dispatcher initDispatcher(HostConfig filterConfig) {
  Dispatcher dispatcher = createDispatcher(filterConfig);
  dispatcher.init();
  return dispatcher;
 }

 @SuppressWarnings({ "rawtypes", "unchecked" })
 private Dispatcher createDispatcher(HostConfig filterConfig) {
  Map params = new HashMap();
  String name;
  String value;
  for (Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); params
    .put(name, value)) {
   name = (String) e.next();
   value = filterConfig.getInitParameter(name);
  }
  return new TestDispatcher(filterConfig.getServletContext(), params);
 }

 

3.其中的TestDispatcher集成的是Dispatcher,我们可以在init方法中添加相应的加载xml方法

private void init_XmlConfigurations()
 {
  String realPath = this.servletContext.getRealPath("/");
  String configDir = this.initParams.get(“configDir”);
  if (configDir == null)
  {
   configDir = java.io.File.separator;
   configDir = realPath + configDir;
  }

  String configFilenamePattern = this.initParams
    .get("configFilenamePattern");
  if (configFilenamePattern == null)
  {
   configFilenamePattern = "struts*.xml";
  }

  this.getConfigurationManager().addConfigurationProvider(
    new TestConfigurationProvider(configDir, configFilenamePattern,
      false, servletContext));
 }

 

public void  init()

{

   ....

   init_XmlConfigurations();

   ....

}

 

4.TestConfigurationProvider 集成的是StrutsXmlConfigurationProvider,重写getConfigurationUrls方法

 

public TestConfigurationProvider (String baseDir, String filenamePattern,
   boolean errorIfMissing, ServletContext servletContext)
 {
  super(filenamePattern, errorIfMissing, servletContext);
  if (baseDir != null)
  {
   this.baseDir = new File(baseDir);
  }


  this.filenamePattern = filenamePattern;
  this.servletContext = servletContext;
 }

 

 @Override
 protected URL findInFileSystem(String fileName) throws IOException
 {
  URL url = null;
  File file = new File(fileName);

  if (!file.exists())
  {
   file = new File(baseDir, fileName);
  }
  if (file.exists())
  {
   try
   {
    url = file.toURL();
   }
   catch (MalformedURLException e)
   {
    throw new IOException("Unable to convert " + file + " to a URL");
   }
  }
  return url;
 }

 /*
 @Override
 protected Iterator<URL> getConfigurationUrls(String filenamePattern)
   throws IOException
 {
  List<URL> list = new ArrayList<URL>();
  URL url = null;
  if (this.baseDir != null && this.baseDir.exists()
    && this.baseDir.isDirectory())
  {
   String[] struts2Files = baseDir.list(new TestStruts2FilenameFilter(
     filenamePattern));
   for (String fileName : struts2Files)
   {
    url = findInFileSystem(fileName);
    if (url == null)
    {
     return super.getConfigurationUrls(fileName);
    }
    else
    {
     list.add(url);
    }
   }
  }
  return list.iterator();
 }

5.编写一个文件名称的匹配类TestStruts2FilenameFilter

 private String pattern;
 private List<Pattern> rules =new ArrayList();;
  public MostStruts2FilenameFilter(String pattern)
 {
  if (pattern == null || pattern.length() == 0)
  {
   this.pattern ="*.xml";
  }
  else
  {
   this.pattern = pattern;
  }
  String[] as = this.pattern.split("[ ,;]+");
  for (int i = 0; i < as.length; i++)
  {
   if (as[i].length() > 0)
   {
    String regx = as[i].replaceAll("\\*", "(\\\\S)*");
    rules.add(Pattern.compile(regx,
      Pattern.CASE_INSENSITIVE));
   }
  }
 }

 

 public boolean accept(File dir, String filename)
 {
  boolean flag = false;
  for (int i = 0; !flag && i < rules.size(); i++)
  {
   Pattern patternrule = (Pattern) rules.get(i);
   flag = patternrule.matcher(filename).matches();
  }
  return flag;
 }

 

这样我们就完成了动态配置struts配置文件的,从而实现了struts配置多个文件。

你可能感兴趣的:(struts2)