Struts2 Configuration file and its roles
Struts 2 application is mainly dependent on two configuration files.
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>
Do read :
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>