Home Struts Struts Struts2.2.1 Databaseapps Writing action classes in struts2.2.1

Ask Questions?

View Latest Questions


 
 

Writing action classes in struts2.2.1
Posted on: February 5, 2011 at 12:00 AM
Advertisement
In this section, you will see the how to create action class in struts appliction.

Writing action classes

Action Classes

AdmissionAction.java-  It action class for admission process. It takes
data from admission form and process for registration. There is a execute
method which holds connection code for connection to database,
and insert data into database.

AdmissionAction.java

package net.roseindia.action;

import net.roseindia.dao.ApplicationDAO;
import net.roseindia.model.StudentAdmissionModel;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class AdmissionAction extends ActionSupport implements
		ModelDriven<StudentAdmissionModel> {
	private static final long serialVersionUID = 1L;
	StudentAdmissionModel admissionModel;
	ApplicationDAO student = new ApplicationDAO();

	public String execute() throws Exception {
		if(admissionModel!=null){
		student.add(admissionModel);
		return SUCCESS;
		}
		else{
			return INPUT;
		}
	}

	@Override
	public StudentAdmissionModel getModel() {
		admissionModel = new StudentAdmissionModel();
		return admissionModel;
	}
}

SearchAction.java

package net.roseindia.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import net.roseindia.dao.ApplicationDAO;
import net.roseindia.model.SearchModel;
import net.roseindia.model.StudentAdmissionModel;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class SearchAction extends ActionSupport implements ModelDriven<SearchModel>{
	private static final long serialVersionUID = 1L;
	SearchModel searchModel;
	HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
	public SearchAction() {
	}

	public String execute() throws Exception {
		ApplicationDAO student=new ApplicationDAO();
		
		if(searchModel.getS_roll()!=null && searchModel.getS_name()==null && searchModel.getCalss1()==null){		
		int roll=Integer.parseInt(searchModel.getS_roll());
		StudentAdmissionModel model=student.search(roll);
		request.setAttribute("data", model);
		}
		else{
			System.out.println("Inside Model");
			System.out.println(searchModel.getCalss1());
			StudentAdmissionModel model=student.search(searchModel);
			request.setAttribute("data", model);
		}
		//session.("data", model);
		return SUCCESS;
	}

	public SearchModel getModel() {
		searchModel = new SearchModel();
		return searchModel;
	}	
}

UpdateAction.java

package net.roseindia.action;

import net.roseindia.dao.ApplicationDAO;
import net.roseindia.model.UpdateModel;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UpdateAction extends ActionSupport implements ModelDriven<UpdateModel> {

	private static final long serialVersionUID = 1L;
	UpdateModel model;

	public String execute() throws Exception {
		if(!model.equals(null)){
			ApplicationDAO student = new ApplicationDAO();
			student.update(model);			
			return SUCCESS;
		}
		else{
			return INPUT;
		}
	}

	@Override
	public UpdateModel getModel() {
		model = new UpdateModel();
		return model;
	}
		
}

SearchForDelete.java

package net.roseindia.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.roseindia.dao.ApplicationDAO;
import net.roseindia.model.SearchModel;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class SearchForDelete extends ActionSupport implements
		ModelDriven<Object>, SessionAware {
	SearchModel obDeleteModel;
	List<String> dataAfterDeletion = new ArrayList<String>();
	Map session;
	ApplicationDAO student = new ApplicationDAO();
	HttpServletRequest request = (HttpServletRequest) ActionContext
			.getContext().get(ServletActionContext.HTTP_REQUEST);
	String sRoll = request.getParameter("id");
	int roll = Integer.parseInt(sRoll);

	public String execute() throws Exception {
		System.out.println(roll);
		student.delete(roll);
		return SUCCESS;
	}

	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		obDeleteModel = new SearchModel();
		return obDeleteModel;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

}

Related Tags for Writing action classes in struts2.2.1:


DMCA.com