Pages Navigation Menu

Coding is much easier than you think

Struts2 Configuration file and its roles

Struts2 Configuration file and its roles

 
Struts 2 application is mainly dependent on two configuration files.

  1. web.xml
  2. struts.xml

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 .

 
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.
 

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

 

About Jamil


Am currently working as a Struts 2, Webservices Developer in a reputed IT Organisations. I can help you with teaching Core java , Struts 2 and Webservices.

How to Reload a Page Only Once Using Javascript
Converting XLS to CSV files Using Java

Leave a Reply

SimpleCodeStuffs located at 511/67 Huynh Van Banh , Ho Chi Minh, VN . Reviewed by 56 customers rated: 4 / 5