java 在 web.xml 中包含文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6592035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:30:59  来源:igfitidea点击:

Including files in web.xml

javajakarta-eeweb.xml

提问by Catchwa

I'm in the process of writing unit tests for a JEE5 web service. The behaviour of the web service depends on the attributes set in the web.xmlfile. I'm wanting to therefore split my web.xmlinto a constant part and a part that is changed around inbetween test runs.

我正在为 JEE5 Web 服务编写单元测试。Web 服务的行为取决于web.xml文件中设置的属性。因此,我想将我的web.xml分为一个恒定部分和一个在测试运行之间改变的部分。

To see if it's actually possible, I've tried to see if I can split out the welcome-file-listattribute. Using some instructions I foundI've come up the following:

为了看看它是否真的可能,我试图看看我是否可以拆分welcome-file-list属性。使用一些说明,我发现我提出了以下内容:

web.xml

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" [
  <!ENTITY fragment SYSTEM "fragment.xml">
]>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>NewWebService</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>NewWebService</servlet-name>
        <url-pattern>/NewWebService</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
  &fragment;
</web-app>

fragment.xml

片段.xml

<?xml version="1.0" encoding="UTF-8"?>
<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

However, I'm getting validation errors on the web.xmlfile:

但是,我收到web.xml文件验证错误:

Attribute "version" must be declared for element type "web-app". [7]
Attribute "xmlns" must be declared for element type "web-app". [7]
Attribute "xmlns:xsi" must be declared for element type "web-app". [7]
Attribute "xsi:schemaLocation" must be declared for element type "web-app". [7]

必须为元素类型“web-app”声明属性“version”。[7]
必须为元素类型“web-app”声明属性“xmlns”。[7]
必须为元素类型“web-app”声明属性“xmlns:xsi”。[7]
必须为元素类型“web-app”声明属性“xsi:schemaLocation”。[7]

I get the feeling that using a web app v2.3 DTD and a web app v2.5 schema inside the same file is the problem, but I don't know how I'm going to be able to get around it.

我觉得在同一个文件中使用 Web 应用程序 v2.3 DTD 和 Web 应用程序 v2.5 架构是问题所在,但我不知道如何解决它。

(Any other approaches in splitting a web.xml file into smaller chunks would be welcome too!)

(将 web.xml 文件拆分为更小的块的任何其他方法也将受到欢迎!)

Update

更新

If I remove the DTD reference like so...

如果我像这样删除 DTD 引用...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app[
  <!ENTITY fragment SYSTEM "fragment.xml">
]>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
etc, etc, etc.

...it appears as though the validation process ignores the web-app_2_5.xsdfile:

...似乎验证过程忽略了该web-app_2_5.xsd文件:

Element type "web-app" must be declared. [5]
Element type "listener" must be declared. [6]
Element type "listener-class" must be declared. [7]
Element type "servlet" must be declared. [9]
etc, etc, etc.

必须声明元素类型“web-app”。[5]
必须声明元素类型“侦听器”。[6]
必须声明元素类型“listener-class”。[7]
必须声明元素类型“servlet”。[9]
等等等等

采纳答案by Catchwa

I ended up adding a hook into my web service that checks for the presence of an epropertiesfile. If it finds one, it knows that it is in testing mode and instead of using the values specified in web.xml, it pulls them from the properties file. Not very elegant but at least it works. The properties file is copied to the WEB-INFdirectory by the JUnit test during the @BeforeClassmethod. Given the success of this method I've been wondering whether web.xmlis the best place to store web application settings anyway...

我最终在我的 Web 服务中添加了一个钩子,用于检查eproperties文件的存在。如果找到,它就知道它处于测试模式,而不是使用 中指定的值web.xml,而是从属性文件中提取它们。不是很优雅,但至少它有效。WEB-INF@BeforeClass方法期间,JUnit 测试将属性文件复制到目录中。鉴于这种方法的成功,我一直想知道它是否web.xml是存储 Web 应用程序设置的最佳位置……