<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts1</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
In web.xml file, a welcome-file tag indicates to map the JSP page names in configuration file.Struts 2 contain filter tags, like filter-name and filter-class. If one give the filter name and filter-class name in the org.apache.struts2.dispatcher.FilterDispatcher. Then one can see the URL pattern is the given /.action name or not.If the given URL pattern is action name.action. The filterDispatcher class takes the default URL is /.action.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <action name="Hello" class="com.splesson.SPlessonsAction"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
Package element contain some attributes like name,namespace and extend.
Name: It specifies the package name. By default, it as the default package name.
package name="default"
Namespace: It specifies the action class location or uri, It is not mandatory in struts.XML file. The container will take the default namespace uri */.action.
Extends: It is used, whenever one extended any class or implements any interface, at that time the extends is used. The container will take the default package struts-default.
3. action element is defined as the action class properties. It contains three attributes like name, class and method. an action tag writes under the package tag.
Name: It specifies the action name. A container will check the JSP page action name and action class name is same or not.
action name="Hello"
Class: It is defined as action class name with fully qualified name (package name and class name). A container will check the mapped action class. If action classes matched then execute() method is executed.
action name="action" class="com.itoolsinfo.ActionClass"
Method: It is the optional attribute. When one execute the specific method in Action class. The method attribute is defined in action tag. for example, when the execute() method in Action class defined, It will be executed manually and defined the method= method name.
actionname="myAction"Class="com.itoolsinfo.MyActionClass"method="post"
4. result element is defined as the after execute the execute() method, The container will check, where one want to store that result. It is the sub tag of action class tag.
result element is contained name and value.
Name: Every result contain some specific name and it can map the execute() method return value.
The container will take the default name is a success.
result name="success"
Type: It is optional. It will check whether JSP page response is displayed on a browser.
result name="success">HelloWorld.jsp
Then HelloWorld.jsp page response will be printed on browser.
In struts application, one need to configure the multiple configuration files in classes folder. Classes folder doesn’t create the default in eclipse IDE, one need to create a classes folder in WEB-INF folder.
struts-first.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" namespace="/first"extends="struts-default"> <action name="action" class="com.splesson.ActionClass" > <result name="success">/sucess.jsp</result> </action> </package> </struts>
struts-second.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" namespace="/second" extends="struts-default"> <action name="myaction" class="splesson.MyActionClass" > <result name="failure">failure.jsp</result> </action> </package> </struts>
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"/> <include file="first/struts-first.xml"/> <include file="second/struts-second.xml"/> </struts>
One can write the configuration file by including all Action class in that file.
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <action name="action" class="com.splesson.ActionClass"> <result name="success">sucess.jsp</result> </action> <action name="myAction" class="com.splesson.MyActionClass"> <result name="failure">/failure.jsp</result> </action> </package> </struts>
action element is the sub component of package. It speaks to an activity to be conjured for the approaching request. It has name, class and method attributes. In the event that you don’t determine name property as a matter of course execute() technique will be summoned for the predetermined actiion class.