gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:20:28 25 4
gpt4 key购买 nike

我创建了一个 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 的默认位置。

最佳答案

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

来自 Configuration Files

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

但是,要回答您的问题,可以使用 config 添加默认配置文件以外的配置文件。 web.xml中的初始化参数。

来自 web.xml

Key Initialization Parameters

config - a comma-delimited list of XML configuration files to load.

所以在 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>

也就是说,我通常会避免这种定制:除了潜在的缺点之外,您基本上什么也得不到,因为离开了主干道,您可能是世界上唯一的缺点。

关于java - 在 Struts2 中更改 struts.xml 的默认位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30620190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com