Home Struts Struts Struts2.2.1 Add configuration in struts.xml file

Ask Questions?

View Latest Questions


 
 

Add configuration in struts.xml file
Posted on: December 2, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn how to map/config the struts application in struts.xml

Add configuration in struts.xml file

Struts uses the configuration file (struts.xml) to initialise  its resources such as interceptors, actions and resources. The interceptor may be preprocessor or postprocessor. The resource files is used to prepare views, templates and velocity. The struts.xml file is core configuration file for struts application.

Before configuring the struts.xml file you also need to do filter mapping in web.xml file. This filter is the StrutsPrepareAndExecuteFilter. An example of mapped web.xml file is given below

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app 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"
version="2.5">

<display-name>Struts2 JSP Example</display-name>

<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> 

</web-app>

The following is the structure of the struts.xml file

<?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>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<!-- Package Mapping -->

<package name="roseindia" namespace="/roseindia" extends="struts-default">

<!-- Action Mapping -->

<action name="helloWorld" class="net.roseindia.HelloWorld">
<result>/jsp/HelloWorld.jsp</result>
</action>

</package>

</struts>

The struts.xml file must confirmed that it is using DTD Struts Configuration 2.0. This file provides the information about the elements and structure that the struts.xml file should have.

Here the package name is the roseindia you can give any name to the package. Package is the way to group the action, interceptors and views together in a logical unit.

The action tag contains the name of the action, class and results which forwards the action by matching there names.

The Mapping for above action can be done as

<?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>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="roseindia" namespace="/roseindia" extends="struts-default">

<action name="loginPage">
<result>/jsp/test.jsp</result>
</action>

<action name="doLogin" class="net.roseindia.SimpleAction">
<result name="error">/jsp/test.jsp</result>
<result name="success">/jsp/home.jsp</result>
</action>

</package>

</struts>

Here net.roseindia is the full package path of SimpleAction class, and its action name is given "doLogin". The resul forwarded the control to the test.jsp and home.jsp when its matching is found.

Download this example code

Related Tags for Add configuration in struts.xml file:


DMCA.com