Struts - SPLessons

Struts 2 Configuration

Home > Lesson > Chapter 5
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Struts 2 Configuration

Struts 2 Configuration

shape Description

Struts2 Configuration file is one of the main assert to develop applications and utilized to view pages and the request is forwarded to action class, at the same time the container create the configuration file.The Struts2 application contains two configuration files like web.xml and struts.xml.web.xml is defined in WEB-INF folder. The user gives the request from the browser, then the Content can be seen in the web.xml. Struts.xml file will be placed under the source folder and remaining all the xml should be placed in WEB-INF folder.

shape web.xml

[xml]<?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>[/xml] 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.

shape struts.xml

Struts.XML  Configuration file is used to map the Action class in view pages. [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="Hello" class="com.splesson.SPlessonsAction"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts> [/xml]
  1. DOCTYPE is used, to map the struts  tags in the configuration file.
  2. One have to write the struts application is some modules, then all modules are configured the one configuration file, at that time package element is used in struts.XML file.
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.

Multiple Action classes

shape Description

In struts application is defined as the Multiple Action classes, all Action classes is configure the struts-XML on the 2 types.

Write the multiple configuration files

shape Example

To configure the multiple configuration files in one application, then one need to give the different names of an xml file.For Example, when one configure the three configuration files likestruts.xml,struts-first.xml,struts-second.xml.One can defined the configuration files like below. struts-first.xml [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> [/xml] struts-second.xml [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> [/xml] struts.xml [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> [/xml] One can write the configuration file by including all Action class in that file. struts.xml [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>[/xml] 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.

Summary

shape Key Points

  • XML configuration files are main assert of the Struts
  • Struts.xml file will be placed under the source directory.