Change default struts.xml location in Struts2

I created a Struts2 project that works great when I put the file struts.xml

in the directory src

.

Below is my configuration web.xml

.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"      
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         id="WebApp_ID" version="3.0">    
    <display-name>struts2project</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

      

If I put mine struts.xml

in a directory WEB-INF

instead, it won't load.

Please don't answer like "it doesn't matter" or anything else. I'm just asking if it is possible (and how) to change the default location struts.xml

or not.

+3


source to share


4 answers


To be completely clear you don't even need struts.xml, the only config file required is web.xml.

From Configuration Files

From a Struts developer's point of view, the required configuration file used by the framework is web.xml . From here, you have complete control over how Struts configures itself and your application. By default, Struts will load a set of internal configuration files to configure itself, then another set to customize your application, however you can build an entire Struts application without writing a single configuration file other than web.xml .

[...]

File      Optional    Location (relative to webapp)          Purpose
-----------------------------------------------------------------------------------------
web.xml      no           /WEB-INF/                 Web deployment descriptor to include 
                                                    all necessary framework components
-----------------------------------------------------------------------------------------
struts.xml   yes          /WEB-INF/classes/         Main configuration, contains 
                                                    result/view types, action mappings,
                                                    interceptors, and so forth

      

To answer your question, you can add non-standard configuration files using the initialization parameter config

in web.xml.

From web.xml

Key initialization parameters

config

- comma-separated list of XML configuration files to load.

So, it's enough to specify the new struts.xml in the web.xml to achieve your goal:



<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
    <init-param>
        <param-name>config</param-name>
        <!-- Edit after @AleskandrM comments/answer because 
             1) every possible configuration file must be specified,
                otherwise they will be hidden by this setting and 
             2) settings are relative to /WEB-INF/classes/, and hence
                the /WEB-INF/ must be replaced by ../ and
             3) the order is important as well. You cannot extend 
                struts-default package in your struts.xml if it isn't loaded yet. 
        -->

      

        <param-value>/WEB-INF/struts.xml</param-value>

      

Strike>

        <param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
    </init-param>
</filter>

      

However, I generally avoid such settings: you won't earn basically anything other than potential disadvantages that you could only get in the world from leaving the main road.

+4


source


Correct setting to place struts.xml

directly in WEB-INF folder:

<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,../struts.xml</param-value>
    </init-param>
</filter>

      



Read Andrea's answer and to find out why.

The order in which you define the xml files is also very important. For example. You cannot expand a struts-default

(from struts-default.xml

) package struts.xml

if it has not already been downloaded.

+3


source


struts.xml should only be in the application's classpath. Its location doesn't matter.

0


source


Struts 2 loads default configuration files (struts-default.xml, struts-plugin.xml, struts.xml) using StrutsPrepareAndExecuteFilter (org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter).

To understand about uploading these files using the framework -

These filenames are defined as constants in the Dispatcher.class file (used by StrutsPrepareAndExecuteFilter) in the Struts structure.

DEFAULT_CONFIGURATION_PATHS = "struts-default.xml, struts-plugin.xml, struts.xml";

init_TraditionalXmlConfigurations () method present in Dispatcher.class loads these xmls.

private void init_TraditionalXmlConfigurations() {
        String configPaths = initParams.get("config");
        if (configPaths == null) {
            configPaths = DEFAULT_CONFIGURATION_PATHS;
        }
        String[] files = configPaths.split("\\s*[,]\\s*");
        for (String file : files) {
            if (file.endsWith(".xml")) {
                configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));
            } else {
                throw new IllegalArgumentException("Invalid configuration file name");
            }
        }
    }

      

To override the default path parameter for these xmls, you can specify the paths of these XML files when defining the StrutsPrepareAndExecuteFilter in the web.xml, as other users will answer.

0


source







All Articles