employeedispatchaction.java

来自「struts做得固定资产管理系统 带毕业论文完整版」· Java 代码 · 共 182 行

JAVA
182
字号
package com.qrsx.appcam.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.qrsx.appcam.dao.DeptmentDAO;
import com.qrsx.appcam.dao.EmployeeDAO;
import com.qrsx.appcam.dao.RoleDAO;
import com.qrsx.appcam.form.EmployeeForm;
import com.qrsx.appcam.model.Deptment;
import com.qrsx.appcam.model.Employee;
import com.qrsx.appcam.model.Role;

public class EmployeeDispatchAction extends BaseDispatchAction {

	/**
	 * 员工信息:保存 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		// 进行有效性验证,如果有错,这在返回编辑页面进行编辑
		ActionErrors errors = form.validate(mapping, request);
		if (!errors.isEmpty()) {

			saveErrors(request, errors);
			return edit(mapping, form, request, response);
		}
		Employee employee = new Employee();
		EmployeeForm employeeForm = (EmployeeForm) form;
		// 获取表单数据,把表单映射到对象中
		BeanUtils.copyProperties(employee, employeeForm);
		EmployeeDAO dao = new EmployeeDAO();
		//判断删除对象Id是否为空,若为空则执行创建操作,否则执行更新操作
		if (employeeForm.getId() == null
				|| employeeForm.getId().trim().length() == 0) {

			dao.create(employee);
			saveMessage(request, "employeeForm.added", employee.getName());
		} else {
			dao.update(employee);
			saveMessage(request, "employeeForm.updated", employee.getName());
		}

		return mapping.findForward("success");

	}

	/**
	 * 编辑员工信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		String id = request.getParameter("id");		
		if (id != null && id.trim().length() > 0) {

			EmployeeDAO dao = new EmployeeDAO();
			//获取需要编辑的对象信息
			Employee employee = dao.findById(Integer.valueOf(id));

			if (employee != null) {
				//把该对象的信息映射到表单中去
				BeanUtils.copyProperties(form, employee);
			}
		}
		// 把deptment数据表部门中的数据添加到list中,并返回到页面上
		DeptmentDAO deptmentDAO = new DeptmentDAO();
		List<Deptment> list1 = new ArrayList<Deptment>();
		list1 = deptmentDAO.findAll();
		request.setAttribute("deptments", list1);
		// 把role数据表角色中的数据添加到list中,并返回到页面上
		RoleDAO roleDAO = new RoleDAO();
		List<Role> list2 = new ArrayList<Role>();
		list2 = roleDAO.findAll();
		request.setAttribute("roles", list2);

		return mapping.findForward("edit");
	}

	/**
	 * 删除员工信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		String id = request.getParameter("id");
		if (id != null || id.trim().length() > 0) {

			EmployeeDAO dao = new EmployeeDAO();
			dao.delete(Integer.valueOf(id));//执行删除操作
			saveMessage(request, "employeeForm.deleted");
		}

		return mapping.findForward("success");
	}

	/**
	 * 根据条件动态检索员工信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		Employee employee = new Employee();
		BeanUtils.copyProperties(employee, form);
		// 把员工所在部门列表放到request中返回页面
		DeptmentDAO deptmentDao = new DeptmentDAO();
		List<Deptment> deptmentList = deptmentDao.findAll();
		request.setAttribute("deptments", deptmentList);
		// 把员工的角色列表放到request中返回页面
		RoleDAO roleDao = new RoleDAO();
		List<Role> roleList = roleDao.findAll();
		request.setAttribute("roles", roleList);
		// 根据条件检索员工信息,并把员工列表放到request中返回
		EmployeeDAO dao = new EmployeeDAO();
		List<Employee> list = dao.list(employee);
		request.setAttribute("employees", list);

		return mapping.findForward("list");
	}

	/**
	 * 检索所有员工的信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward findAll(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		EmployeeDAO dao = new EmployeeDAO();

		List<Employee> list = dao.findAll();
		request.setAttribute("employees", list);

		return mapping.findForward("list");
	}
}

⌨️ 快捷键说明

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