Struts.xml file:
The struts.xml is the core configuration file for struts framework. The struts.xml is used to initialize the resources like Interceptors, Action classes and Results.
Syntax:
<struts> <package name="packageName" extends="struts-default"> <action name="actionName" class="actionClass" method="methodName"> <result name="success">/HelloWorld.jsp</result> </action> //more actions. </package> //more packages. </struts> |
Commonly used attributes:
1. package attribute: It is used for modularization. You can divide configuration into multiple modules.
2. action attribute: It is a sub-attribute of the package attribute. It represents an action.
3. result attribute: It is a sub-attribute of the action attribute. It is used to control the flow after an action is executed.
web.xml:
The web.xml is known as deployment descriptor. It is used for providing entry point in any web application. In struts 2 we have to define FilterDispatcher class entry in web.xml.
web.xml file example:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app> |
Next Topic: Action in struts 2.
Previous Topic: Struts 2 Architecture.