deptmentdispatchaction.java
来自「struts做得固定资产管理系统 带毕业论文完整版」· Java 代码 · 共 150 行
JAVA
150 行
package com.qrsx.appcam.action;
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.form.DeptmentForm;
import com.qrsx.appcam.model.Deptment;
public class DeptmentDispatchAction extends BaseDispatchAction {
/**
* 保存部门信息:更新、创建
*
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
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 mapping.findForward("edit");
}
// 获取表单数据,并把表单中的数据映射到对象中
DeptmentForm deptmentForm = (DeptmentForm) form;
Deptment deptment = new Deptment();
BeanUtils.copyProperties(deptment, form);
DeptmentDAO dao = new DeptmentDAO();
// 判断deptmentForm的Id是否为空,若为空则执行创建操作,否则执行更新操作
if (deptmentForm.getId() == null
|| deptmentForm.getId().trim().length() == 0) {
dao.create(deptment);
saveMessage(request, "deptmentForm.added", deptmentForm.getName());
} else {
dao.update(deptment);
saveMessage(request, "deptmentForm.updated", deptmentForm.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) {
DeptmentDAO dao = new DeptmentDAO();
// 获取要编辑的数据对象信息
Deptment deptment = dao.findById(Integer.valueOf(id));
if (deptment != null) {
// 把该对象的数据映射到表单中
BeanUtils.copyProperties(form, deptment);
}
}
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) {
DeptmentDAO dao = new DeptmentDAO();
dao.delete(Integer.valueOf(id));// 执行删除操作
saveMessage(request, "deptmentForm.deleted");
}
return mapping.findForward("success");
}
/**
* 动态检索部门信息
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
// 把检索条件映射到对象中
Deptment deptment = new Deptment();
BeanUtils.copyProperties(deptment, form);
// 执行检索操作,并把检索的结果返回list页面
DeptmentDAO dao = new DeptmentDAO();
List<Deptment> list = dao.list(deptment);
request.setAttribute("deptments", list);
return mapping.findForward("list");
}
/**
* 检索所有的部门信息
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward findAll(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
DeptmentDAO dao = new DeptmentDAO();
List<Deptment> list = dao.findAll();
request.setAttribute("deptments", list);
return mapping.findForward("list");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?