Home Struts Struts Struts2.2.1 Configuring Actions in Struts application

Ask Questions?

View Latest Questions


 
 

Configuring Actions in Struts application
Posted on: November 24, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn how how to configure/map Action in struts application

Configuring Actions in Struts Application

To Configure an action in struts application, at first write a simple Action class such as

SimpleAction.java

package net.roseindia;

import com.opensymphony.xwork2.Action;

public class SimpleAction implements Action{
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
}

And also write any JSP or HTML page

homepage.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Home Page </TITLE>
</HEAD>
<BODY bgcolor=pink>
<center>
<font size=20 face=modern color=green>This is the some page</font>
</center>
</BODY>
</HTML>

Now its time to map the above files into an application. Here SimpleAction.java is your Action class which returns the success. Now Write the following code into the struts.xml file for action mapping. Before mapping action let I introduce you with the some elemets of struts.xml for action mapping.

<struts>  </struts>  All the mapping is done within this tag.

The parameter struts.enable.DynamicMethodInvocation in <constant name="struts.enable.DynamicMethodInvocation" value="true" /> allows the dynamic method invocation.

The code struts.devMode <constant name="struts.devMode" value="true" /> enable the development mode. When it is enabled the struts becomes much friendlier and provide significant speed  in developments.

The tag <package name="roseindia" extends="struts-default"> </package> defines the name of the package. Here the package roseindia extends the the struts default package.

<action name="simpleAction" class="net.roseindia.SimpleAction" >
<result name="success">/homepage.html</result>
</action>

Now this tag does your action mapping. Here name defines the name of the action, class defines the name of the class with complete package. the <result name="success"></result> defines the action taken by the action class. The element within result matches the string returned by the mapped action. The action mapped is called by the simpleAction.action.

Now a complete struts.xml file is given below

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>

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

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

<action name="simpleAction" 
class="net.roseindia.SimpleAction" >
<result name="success">/homepage.html</result>
</action>

</package>

</struts>

Save this struts.xml file within webapps\actionmapping\WEB-INF\src\java directory. you are recommended to follow the following hierachy


When you run this application it will display message as shown below:


 

Download this example code

Related Tags for Configuring Actions in Struts application:


DMCA.com