deptaction.java

来自「struts+spring +hibernate查询分页」· Java 代码 · 共 106 行

JAVA
106
字号
package com.baidu.struts.ation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.commons.beanutils.*;

import com.baidu.base.BaseAction;
import com.baidu.base.BaseForm;
import com.baidu.struts.form.DeptForm;
import com.baidu.hibernate.pojo.*;
import com.baidu.service.*;

public class DeptAction extends BaseAction {

	// get service for some method
	private IDeptService getService(String serviceName) {
		return ((IDeptService) this.getBean("deptService"));
	}

	// all the method
	public ActionForward createDept(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DeptForm df = (DeptForm) form;
		Zhangshan zs = new Zhangshan();
		BeanUtils.copyProperties(zs, df);
		((IDeptService) this.getBean("deptService")).saveDept(zs);
		return new ActionForward("/dept.do?method=viewDept");
	}

	public ActionForward deleteDept(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Short id = new Short(request.getParameter("id"));
		getService("deptService").deleteDept(id);
		return new ActionForward("/dept.do?method=viewDept");
	}

	public ActionForward findDept(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int page = Integer.valueOf(request.getParameter("page"));
		request.setAttribute("deptList", ((IDeptService) this
				.getBean("deptService")).getResultsByPage(page * 10, 10));
		request.setAttribute("totalPage", new Integer(((IDeptService) this
				.getBean("deptService")).getTotalPage(10)));
		request.setAttribute("currentPage", request.getParameter("page"));
		
		request.setAttribute("totalRecord", new Integer(getService("deptService").getTotalRecords()));
		request.setAttribute("startIndex",page * 10+1);
		
		int temp = getService("deptService").getTotalRecords();
		if (temp>page * 10+10){
			temp = page*10+10;
		}
		
		request.setAttribute("endIndex", temp);
		
		return mapping.findForward("deptlist");
	}

	public ActionForward findOneDept(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("uddept",getService("deptService").findById(new Short(request.getParameter("id"))));
		return mapping.findForward("update");
	}

	
	public ActionForward updateDept(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		DeptForm df = (DeptForm) form;
		Zhangshan zs = getService("deptService").findById(new Short(request.getParameter("id")));
		zs.setFldDeptAddress(df.getFldDeptAddress());
		zs.setFldDeptDrop(df.getFldDeptDrop());
		zs.setFldDeptIndex(df.getFldDeptIndex());
		zs.setFldDeptMemo(df.getFldDeptMemo());
		zs.setFldDeptName(df.getFldDeptName());
		zs.setFldDeptParent(df.getFldDeptParent());
		zs.setFldDeptPhone(df.getFldDeptPhone());
		zs.setFldDeptType(df.getFldDeptType());
		getService("deptService").updateDept(zs);
		return new ActionForward("/dept.do?method=viewDept");
	}

	public ActionForward viewDept(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("come to viewDept");
		request.setAttribute("deptList", ((IDeptService) this
				.getBean("deptService")).getResultsByPage(0, 10));
		request.setAttribute("totalPage", new Integer(((IDeptService) this
				.getBean("deptService")).getTotalPage(10)));
		request.setAttribute("currentPage", new Integer("0"));
		request.setAttribute("totalRecord", new Integer(getService("deptService").getTotalRecords()));
		
		request.setAttribute("startIndex", new Integer(1));
		int temp = getService("deptService").getTotalRecords();
		if (temp>10){
			temp = 10;
		}
		request.setAttribute("endIndex", new Integer(temp));
		return mapping.findForward("deptlist");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?