pderaaij
pderaaij

Reputation: 1337

Missing elements from web-fragment.xml in the effective web.xml

In our project we make use of web-fragments to define some servlets so the artifacts easily can be used in other projects.

Strange thing now is that we have a web-fragment.xml, but some of its contents doesn't get added to the effective web.xml.

By example:

The following configurations is present in the effective web.xml:

<filter>
    <filter-name>superUserAutomaticLogon</filter-name>
    <filter-class>nl.caiw.cool.util.filters.SuperUserAutomaticLogonFilter</filter-class>
    <async-supported>false</async-supported>
</filter>

But the following isn't:

<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/toolbox/modules/*</url-pattern>
</filter-mapping>

We have tried several things, but we can't figure it out. Hopefully someone here can send us in the right direction.

Thanks in advance.

Upvotes: 13

Views: 9801

Answers (3)

EricS
EricS

Reputation: 618

I believe you were running into what I explained in answer to this question.

servet-mappings with the same servlet-name and filter-mappings with the same filter-name occurring in web-fragments are overridden by those in web.xml. They are only additive across web-fragments.

Upvotes: 0

adarshr
adarshr

Reputation: 62603

Strange thing now is that we have a web-fragment.xml, but some of its contents doesn't get added to the effective web.xml.

Well first of all, a web-fragment.xml wouldn't get physically included into the main web.xml. So when you say it doesn't get added to the effective web.xml, I feel you may be going wrong there.

Web fragments allow compoments to register themselves to the main web.xml at runtime. The only evidence you will find of this happening is by actually trying to use the compoment.

I tried out a simple hello-world example with web fragments and got it to work. I used Tomcat 7 for this with a Servlet 3.0 webapp.

My main web.xml inside WEB-INF looks like the below:

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>hello-world</display-name>

    <absolute-ordering>
        <name>filters</name>
    </absolute-ordering>
</web-app>

I figured out that the only way we could register a web-fragment.xml was by adhering to the below rules:

  1. It must be named web-fragment.xml
  2. It must be placed in a JAR file inside WEB-INF/lib
  3. The web-fragment.xml must be present inside the META-INF directory of the JAR file.

My web-fragment.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<web-fragment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd" id="WebAppFragment_ID" version="3.0">
    <name>filters</name>
    <filter>
        <filter-name>interceptor</filter-name>
        <filter-class>com.adarshr.Interceptor</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>interceptor</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-fragment>

With the above minimal setup, I was able to get the Interceptor filter to get fired, though it was placed in the web-fragment.xml.

Finally, here is my directory structure (generated using http://www.adarshr.com/treegen)

+- hello-world
   |
   +- WEB-INF
       |
       +- lib
       |   |
       |   |- my-library.jar
       |
       |- web.xml

The JAR file my-library.jar has the below structure:

+- my-library.jar
   |
   +- META-INF
       |
       |- web-fragment.xml

Some references:

Upvotes: 14

Beilei Huang
Beilei Huang

Reputation: 41

Maybe you can check on web.xml that metadata-complete="true" is not set. According to the Servlet spec if this flag is set to true, it will not load web-fragment.xml or any annotation such as @WebFilter

Upvotes: 4

Related Questions