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.
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 .
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.
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:
web-fragment.xml
WEB-INF/lib
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With