When ever we are developing struts based application we have to do some configuration code changes. It includes the following:
1) web.xml- we have to specify the controler which will handle each request and determine the action to process the request.
Code Sample:
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> <init-param> <param-name>struts.devMode</param-name> <param-value>true</param-valu>> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2) struts.xml and struts.properties- Most of the time we do not use struts.properties, it is used to override the default properties.
In struts.xml we specify a lot of things like package, action, interceptors, results, global-exception esult, constant properties etc.
Example:
<struts> <!-- Package: We can have multiple packages with different namespace --> <package name="ankit" namespace="/" extends="struts-default"> <!-- Global Result--> <global-results> <result name="exception">jsp/error.jsp</result> </global-results> <!-- Global Exception Mapping--> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="exception" /> </global-exception-mappings> <!-- Action --> <action name="examp" class="com.ankit.struts2.ExampAction" method="execute"> <!-- Interceptor --> <interceptor-ref name="params"/> <!-- Result --> <result name="success">/Output.jsp</result> </action> </package> </struts>
We can have multiple file for configuration, so that we can easily maintain the code in seperate packages:
In main struts.xml we can have below entry:
<package name="def" namespace="/" extends="struts-default"> </package> <include file="a/struts-a.xml"></include> <include file="a/struts-b.xml"></include>
Here struts-1.xml can have specification for a functionality and struts-b.xml will have specification for b functionality.