Writing Actions
Posted on: March 3, 2011 at 12:00 AM
Writing Actions

Writing Actions

The Action is responsible for controlling of data flow within an application. You can make any java class as action. But struts have some built in interface and class for making action easily.

To make an Action class using struts you can either user Action interface or use ActionSupport class. Both provides a execute method. By default the execution of action is start from this method. You can write your own method, for this you have to specify the method name in Action mapping in struts.xml file. To get model reference in action class you either you may use ModelDriven interface or simply define attributes and there setter getter methods in action class.

An Example of action class is given belowADS_TO_REPLACE_1

Action class using Action interface

import com.opensymphony.xwork2.Action;

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

Action class using ActionSupport class
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class DemoAction extends ActionSupport implements ModelDriven<Object>{
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}
	
	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		super.validate();
	}
}

The ModelDriven interface return the model of a view.  The validate method is of ActionSupport class. This method is used for the validation of model field value.

Related Tags for Writing Actions:

Advertisements

Ads

 
Advertisement null

Ads