Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

tomcat

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.

like image 398
pderaaij Avatar asked Jun 12 '12 08:06

pderaaij


People also ask

What is web fragment xml?

A web fragment is a logical partitioning of the web application in such a way that the frameworks being used within the web application can define all the artifacts without requiring you to edit or add information in the web. xml .

What is absolute ordering web xml?

The <absolute-ordering> element specifies which web fragment JARs (according to the names in their WEB-INF/web-fragment. xml files) have to be scanned for SCIs, fragments and annotations. An empty <absolute-ordering/> element configures that none are to be scanned.


2 Answers

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:

  • https://blogs.oracle.com/swchan/entry/servlet_3_0_web_fragment
  • http://java.sun.com/developer/technicalArticles/JavaEE/JavaEE6Overview_Part2.html
like image 55
adarshr Avatar answered Oct 13 '22 23:10

adarshr


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

like image 40
Beilei Huang Avatar answered Oct 13 '22 21:10

Beilei Huang