更改 Struts2 中 struts.xml 的默认位置 [英] Change default location of struts.xml in Struts2
问题描述
我创建了一个 Struts2 项目,当我将 struts.xml
文件放在 src
目录中时,该项目运行良好.
I've created a Struts2 project which is working fine when I place my struts.xml
file inside src
directory.
下面是我的 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
目录中,它不会被加载.
If I put my struts.xml
inside WEB-INF
directory instead, it is not getting loaded.
请不要以无关紧要"或其他方式给出答案.我只是问我们是否(以及如何)可以更改 struts.xml
的默认位置.
Please do not give answers as "it is irrelevant" or something else. I'm just asking whether (and how) we can change the default location of struts.xml
or not.
推荐答案
完全清楚,你甚至不需要 struts.xml,唯一需要的配置文件是 web.xml.
To be completely clear, you don't even need struts.xml, the only needed configuration file is web.xml.
来自配置文件
从 Struts 开发人员的角度来看,一个必需的配置框架使用的文件是 web.xml.从这里,你有充分的控制 Struts 如何配置自身和您的应用程序.默认情况下,Struts 会加载一组内部配置文件到配置自己,然后另一组来配置您的应用程序,但是可以在不使用的情况下构建整个 Struts 应用程序编写 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
初始化参数添加默认配置文件以外的配置文件.
To answer your question, however, it is possible to add configuration files other than the default ones by using the config
initialization parameter in web.xml.
来自 web.xml
config
- 要加载的 XML 配置文件的逗号分隔列表.
Key Initialization Parameters
config
- a comma-delimited list of XML configuration files to load.
所以在 web.xml 中指定新的 struts.xml 就足以实现你的目标:
So it is enough to specify the new struts.xml in 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'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>
也就是说,我通常会避免这种定制:除了可能因为离开主要道路而成为世界上唯一一个潜在的缺点之外,您基本上什么也得不到.
That said, I generally avoid this kind of customizations: you will earn basically nothing, apart from potential drawbacks that you might be the only one in the world to get, due to having left the main road.
这篇关于更改 Struts2 中 struts.xml 的默认位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!