开发者

Including files in web.xml

开发者 https://www.devze.com 2023-03-17 19:16 出处:网络
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.xml file. I\'m wanting to therefore split my web.xml into a

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.xml file. I'm wanting to therefore split my web.xml into a constant part and a part that is changed around inbetween test runs.

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

web.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 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.xml file:

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]

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.

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

Update

If I remove the DTD reference l开发者_如何学JAVAike so...

<?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.xsd file:

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.


I ended up adding a hook into my web service that checks for the presence of an eproperties file. 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-INF directory by the JUnit test during the @BeforeClass method. Given the success of this method I've been wondering whether web.xml is the best place to store web application settings anyway...

0

精彩评论

暂无评论...
验证码 换一张
取 消