Create Action class
Posted on: December 2, 2010 at 12:00 AM
In this tutorial you will learn how to create a action class in struts2.2.1

Create Action Class

An action is an important portion of web application. It performs the task such validating the user id and password.  To write an action class you need to extend or import the Action classes or interface respectively. All these classes and interfaces present in com.opensymphony.xwork2 package. The commonly used action class is ActionSupport. An example of action class is given below-

SimpleAction.java

package net.roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class SimpleAction extends ActionSupport {
	String userName;
	String password;
	String control = ERROR;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if (getPassword().equalsIgnoreCase("web")) {
			control = SUCCESS;
		} else {
			control = ERROR;
		}
		return control;
	}
}

In the above action class userName and password is two string variable. When the request comes to the action it invokes the execute() method. And all the logic written within this method is executed. The ActionSupport class implements the Action interface and Action interface provides some static fields such as ERROR, INPUT, LOGIN, NONE, SUCCESS. These fields have used in the above class to forward the action to the next page. All the mapping of Action classes are done into the struts.xml file. You will learn how to map the action in struts.xml in the next tutorial.

Download this example code

Related Tags for Create Action class:

Advertisements

Ads

Ads

 
Advertisement null

Ads