Where to put struts.xml

后端 未结 6 1815
死守一世寂寞
死守一世寂寞 2021-02-07 04:41

With Struts2 we have to have struts.xml in the class path, so it no longer works to have it under WEB-INF. So the way I got my project to deploy was to stick it und

6条回答
  •  不思量自难忘°
    2021-02-07 05:13

    I am late to the party, we can configure the struts.xml in any directory in the classpath of the web application, but provide the location using the "config" init parameter of the filter configuration in web.xml as below, if my struts.xml file is in "/com/resources/" directory.

       
            action
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
             
                config
                struts-default.xml,struts-plugin.xml,/com/resources/struts.xml
             
        
    

    If we don't provide a config init parameter struts2 by default takes 3 values "struts-default.xml,struts-plugin.xml,struts.xml", you can see the struts2 Dispatcher class code below which will configure these 3 files to the configuration manager.

    String configPaths = (String)this.initParams.get("config");
    if (configPaths == null) {
      configPaths = "struts-default.xml,struts-plugin.xml,struts.xml";
    }
    String[] files = configPaths.split("\\s*[,]\\s*");
    for (String file : files)
      if (file.endsWith(".xml")) {
        if ("xwork.xml".equals(file))
          this.configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));
        else
          this.configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, this.servletContext));
      }
    

提交回复
热议问题