在Struts2中更改默认的struts.xml位置

3
我创建了一个 Struts2 项目,当我将 struts.xml 文件放在 src 目录中时,它可以正常工作。
下面是我的 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>

如果我把我的struts.xml文件放在WEB-INF目录里面,它就无法被加载。
请不要回答“这是无关紧要的”或其他类似的答案。我只是想知道是否可以(以及如何)更改struts.xml的默认位置。

你之前使用的位置有什么问题吗? - Roman C
他是一个寻求刺激的人 :P - Andrea Ligios
4个回答

4

为了完全清楚,你甚至不需要 struts.xml 文件,唯一必需的配置文件是 web.xml。

来自 配置文件

From a Struts developer point of view, the one required configuration file used by the framework is web.xml. From here, you have full control over how Struts configures both itself and your application. By default, Struts will load a set of internal configuration files to configure itself, then another set to configure your application, however it is possible to 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
回答您的问题,可以使用web.xml中的config初始化参数添加除默认配置文件以外的其他配置文件。
web.xml中得知:
重要的初始化参数有:

config

config - 要加载的逗号分隔的XML配置文件列表。

因此,在web.xml中指定新的struts.xml即可实现您的目标。
<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's 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>

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

话虽如此,我通常避免这种自定义:除了潜在的缺点之外,您基本上不会获得任何东西,因为您可能是唯一一个偏离主路的人。


我认为 /WEB-INF/struts.xml 不会生效,因为位置是相对于 webapp /WEB-INF/classes/ 的。另外请参考https://dev59.com/bWvXa4cB1Zd3GeqPO_2M。 - Aleksandr M
@AleksandrM 你确定需要指定每个配置文件而不仅仅是带有特殊位置的文件吗?使用我的配置,我将会“隐藏” struts-default.xml 和 struts-plugin.xml?对我来说似乎有些过度了 :| - Andrea Ligios
我确定。-> https://github.com/apache/struts/blob/master/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java#L361。 - Aleksandr M
一个愉快的欺骗同事需要插件的方法 :P 当你的被接受后,我会编辑答案并提及你。 - Andrea Ligios
1
...而且顺序也很重要。如果struts-default包没有加载,你就不能在struts.xml中扩展它。 - Aleksandr M
2
也添加了它。为了移动一个不必要的文件到一个本不应允许的地方,真是一团糟:S - Andrea Ligios

3
正确的配置是将 struts.xml 直接放在 WEB-INF 文件夹中:
<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>

阅读Andrea的回答和这个问题,以了解原因。

此外,定义xml文件的顺序也很重要。例如,如果尚未加载,则无法在 struts.xml 中扩展 struts-default 包(来自 struts-default.xml )。


0

Struts 2使用StrutsPrepareAndExecuteFilter(org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter)加载默认配置文件(struts-default.xml,struts-plugin.xml,struts.xml)。

要了解框架如何加载这些文件 -

这些文件名在Struts框架的Dispatcher.class(用于StrutsPrepareAndExecuteFilter)文件中定义为常量。

DEFAULT_CONFIGURATION_PATHS =“struts-default.xml,struts-plugin.xml,struts.xml”;

Dispatcher.class中的init_TraditionalXmlConfigurations()方法加载这些xml文件。

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");
            }
        }
    }

如果要覆盖这些XML的默认路径设置,可以在web.xml中定义StrutsPrepareAndExecuteFilter时提供这些XML的路径,其他用户已经在此回答过了。


0

struts.xml 应该只存在于应用程序的类路径中。它的位置是无关紧要的。


1
你忘记添加或其他内容了。 - Aleksandr M
1
抱歉,我不明白您的意思。 - Rajendra Gupta
2
Please do not give answer as "it is irrelevant" or something else. - Andrea Ligios
@AleksandrM,something else看起来像是一个黑客攻击,应该相应地予以清除。 - Roman C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接