Pages Navigation Menu

Coding is much easier than you think

Struts2 Configuration file and its roles

 
Struts 2 application is mainly dependent on two configuration files.

  1. web.xml
  2. struts.xml

 
Struts2 Configuration file and its roles

 
1. web.xml

web.xml is common for all J2EE application and in case of Struts 2, it is used in the configuration of StrutsPrepareAndExecuteFilter, TilesListener etc.
 
When any request comes for struts web application, that request is first handled by web.xml file, where we do an entry for “StrutsPrepareAndExecuteFilter”, which dispatch a request object to appropriate action name.
 
StrutsPrepareAndExecuteFilter is having following responsibility :

1. Executing struts actions name
2. Cleaning up the ActionContext
3. Serving static content
4. Invoking interceptor chain for the request life-cycle
 
We are defining StrutsPrepareAndExecuteFilter entry in web.xml as:

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

Note : We don’t have to do an entry for taglib of struts in web.xml file because its already included into struts-core.jar file, so container automatically discover it .

 
Do read :


 
2. struts.xml

The struts.xml file contains information about all the Action classes of application, configured interceptors for each Action class, Result for each Action execution. This file also describes the package structure of the application in which different modules are defined. Application specific constant like resource file, devMode, custom extension, theme settings also configured in this file. Below is the sample struts.xml file. All terms will be explained in their respective article.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="welcome" class="com.action.LoginAction">
        <result name="success">success.jsp</result>
        <result name="error">error.jsp</result>
</action>
</package>
</struts>

 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.