Ordering of web fragments

This section contains the detailed description on Ordering of web fragments.

Ordering of web fragments

Ordering of web fragments

This section contains the detailed description on Ordering of web fragments.

The web fragment introduces in Servlet 3.0. It offers less configuration and improved pluggability to developers. A web fragment is a portion or all of the deployment descriptor(web.xml). It can be defined and enclosed in the jar file's META-INF of a framework or library. The container employs the configuration as describe in the web fragment.

A web fragment provide logical division of the web application so that the frameworks used within the web application can specify all the artifacts without requiring developers to edit or add any info in the web.xml. It employed nearly all the elements of the deployment descriptor(web.xml).

Web fragments ordering

More than one web fragment can be employed. Each web fragment describe logical division. All these web fragment should be considered as complete web.xml(deployment descriptor). The web fragment provide capability to the frameworks to auto register to the web container.

In Servlet 3.0 specification, you can order the web fragment in two ways :

  • absolute ordering

  • relative ordering

Absolute ordering of web fragments

Using <absolute-ordering> element in deployment descriptor(web.xml), you can set absolute ordering as follows :

<?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">
	<name>MyApp</name>
	<absolute-ordering>
		<name>MyFragment1</name>
		<name>MyFragment2</name>
	</absolute-ordering>
	...

</web-app>

In the above case, the sequence of ordering will be :

  1. web.xml(It will be always processed first)

  2. MyFragment1

  3. MyFragment2

Relative ordering of web fragments

Using <ordering> element in web-fragment.xml, you can set relative ordering. Given below three different web-fragment.xml files as follows :

web-fragment.xml

<web-fragment>
	<name>MyFragment1</name>
	<ordering><after><name>MyFragment2</name></after></ordering>
	...
</web-fragment>

web-fragment.xml

<web-fragment>
	<name>MyFragment2</name>
	...
</web-fragment>

web-fragment.xml

<web-fragment>
	<name>MyFragment3</name>
	<ordering><before><others/></before></ordering>
	..
</web-fragment>

The <before> specifies that the containing fragment must be ordered before the fragment name provide under nested <name> tag. The <after> tag specifies that the fragment must be ordered after the fragment name provide under nested <name> tag. The <before><others/></before> specifies that the containing fragment must be the first in the ordering after web.xml.

The sequence of web fragments for the above is given below :

  1. MyFragment3

  2. MyFragment2

  3. MyFragment1