This article is a reference on how to enable Struts framework for Java EE applications through configuration in web deployment descriptor file (web.xml). It can be applied for both Struts 1 and Struts 2, using standard configuration techniques defined by Servlet specification like servlet declaration and servlet mapping.

 

1. Enable Struts 1 framework

Struts 1 handles HTTP requests by its own controller servlet called ActionServlet, as depicted in the following diagram:

Struts 1 action servlet diagram

The typical steps to enable Struts in web.xml are pretty simple, as follows:

-          Define Struts action servlet and its initialization parameters.

-          Specify servlet mapping for the action servlet. There are two types of mapping which delegate the matching URLs to be processed by Struts controller:

        • Prefix matching.
        • Extension matching.
And next are the examples.

Define Struts action servlet

The org.apache.struts.action.ActionServletclass is the Struts controller servlet which can be declared in the web.xml file as follows:

<servlet>
    <servlet-name>StrutsController</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
	<param-name>config</param-name>
	<param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
We also declare an initialization parameter named config– which points to the location of Struts configuration files. Multiple configuration files can be used, and they must be separated by comma, for example:

<init-param>
	<param-name>config</param-name>
	<param-value>/WEB-INF/struts-config1.xml, /WEB-INF/struts-config2.xml </param-value>
</init-param>
Beside the commonly used parameter config, Struts also defines several parameters for configuring its controller: chainConfig, config/${module}, configFactory, convertNull, rulesets, validating.



The load-on-startup tag tells the container to load the servlet upon server starts up, instead of loading when invoked as usual convention. The value of 1 means this servlet has highest priority loading. This is necessary for the framework initializes its resources before serving client’s requests.

 

Specify servlet mapping for the action servlet

The second step is to tell the servlet container which kind of incoming URLs will be processed by Struts action servlet, by using <servlet-mapping> element in web.xml file. For example: 

<servlet-mapping>
    <servlet-name>StrutsController</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>
The URL pattern /services/*is called prefix matching which means that all URLs start with the prefix will be processed by the action servlet. For example, the following URLs will match the pattern:

http://localhost/webapp/services/deposit

http://localhost/webapp/services/widthdraw

The second pattern is called extension matching which means that all URLs end with the extension will be processed by the action servlet, for example:

<servlet-mapping>
    <servlet-name>StrutsController</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
The following example URLs will match the extension matching pattern:

http://localhost/webapp/login.do

http://localhost/webapp/listProduct.do

So far a typical configuration for Struts 1 framework in web.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Struts1Application</display-name>

	<!-- Begin Struts 1 config -->
	<servlet>
		<servlet-name>StrutsController</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>StrutsController</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<!-- End Struts 1 config -->
	
</web-app>
 

2. Enable Struts 2 framework

Unlike Struts 1, Struts 2 has different architecture which involves around filters and interceptors. Struts 2’s filter dispatcher is responsible for receiving incoming requests and dispatches them to appropriate action classes. But before reaching action classes, the requests are going through a set of interceptors which modify the requests for specific purpose. The following diagram briefly depicts the workflow of Struts 2 framework:

Struts 2 workflow diagram

For Struts 2 version < 2.1.3, the filter dispatcher is implemented by FilterDispatcherclass.

For Struts 2 version >= 2.1.3, it is recommended to use this dispatcher class: StrutsPrepareAndExecuteFilter, since is the FilterDispatcher class deprecated.

To enable Struts 2 for a Java EE application, it requires adding these entries into web.xml file:

-          Declaration of the filter dispatcher.

-          URL mapping for the filter dispatcher.

And the following is a typical configuration for enabling Struts 2 in the web.xml file: 

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
		http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Strut2Application</display-name>
	
	<!-- Begin Struts 2 config -->
	<filter>
		<filter-name>DispatcherFilter</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>DispatcherFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- End Struts 2 config -->
</web-app>
 

Related Struts Tutorials:

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#3Deepak2022-08-05 11:05
is giving me errors for struts 1 framework.
Quote
#2sravani2017-01-27 01:30
clearly explained and it is best for beginners
Quote
#1ravi2014-07-01 02:26
nice explanation .....
Quote