⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 30d587eb0831001d18349cfc86616cdd

📁 客户关系管理系统主要管理新老客户的一些信息并可以发现潜在客户
💻
字号:
/**
 * 
 */
package com.qrsx.qrsxcrm.action;

import java.util.Iterator;
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.qrsxcrm.dao.UserDAO;
import com.qrsx.qrsxcrm.form.UserForm;
import com.qrsx.qrsxcrm.model.Employee;
import com.qrsx.qrsxcrm.model.Role;
import com.qrsx.qrsxcrm.model.User;
import com.qrsx.qrsxcrm.web.Pager;

/**
 * @author Administrator
 * 
 */
public class UserAction extends BaseDispatchAction {
	@SuppressWarnings("unchecked")
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		 ActionErrors errors=form.validate(mapping,request);
		 if(!errors.isEmpty()){
		 saveErrors(request,errors);
		 return edit(mapping,form,request,response);
		 }
		UserDAO userDAO = new UserDAO(User.class);
		UserForm userForm = (UserForm) form;
		String roleId = userForm.getRoleId();
		String employeeId = userForm.getEmployeeId();
		Role role = (Role) userDAO.findById(Role.class, roleId);
		Employee employee = (Employee) userDAO.findById(Employee.class,
				employeeId);
		User user = new User();
		BeanUtils.copyProperties(user, form);
		user.setRole(role);
		user.setEmployee(employee);
		if (user.getId() == null || user.getId().trim().length() == 0) {
			userDAO.create(user);
			saveMessage(request, "create", user.getUserName());
		} else {
			userDAO.updates(user);
			saveMessage(request, "update", user.getUserName());
		}
		return mapping.findForward("success");
	}

	@SuppressWarnings("unchecked")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		UserDAO userDAO = new UserDAO(User.class);
		User user = new User();
		BeanUtils.copyProperties(user, form);
		UserForm userForm = (UserForm) form;
		String roleId = userForm.getRoleId();
		if (roleId != null && !roleId.trim().equals("")) {
			Role role = (Role) userDAO.findById(Role.class, roleId);
			user.setRole(role);
		}
		List<Role> roles = userDAO.findAll("from Role");
		request.setAttribute("roles", roles);

		try {
			Pager pager = null;
			List results = userDAO.findAll("from User");// 得到总数据

			pager = new Pager(); // 构造分页对象
			int totalRows = results.size(); // 得到总数据量
			pager.init(totalRows);

			if (request.getParameter("action") != null) {
				pager.doAction(request.getParameter("action").toString());
			}
			// 使用分页标签的方法
			List list = userDAO.findAllByPage(user,
					(pager.getCurrentPage() - 1) * pager.getPageSize(), pager
							.getPageSize());
			request.getSession().setAttribute("pagerstruts", pager);
			request.setAttribute("users", list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mapping.findForward("list");
	}

	@SuppressWarnings("unchecked")
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		UserDAO userDAO = new UserDAO(User.class);
		List<Employee> employees = userDAO.findAll("from Employee");
		List<Role> roles = userDAO.findAll("from Role");
		request.setAttribute("employees", employees);
		request.setAttribute("roles", roles);

		if (id != null && id.trim().length() > 0) {

			User user = (User) userDAO.findById(User.class, id);
			if (user.getEmployee() != null) {
				user.setEmployeeId(user.getEmployee().getId());
			}
			if (user.getRole() != null) {
				user.setRoleId(user.getRole().getId());
			}
			if (user != null) {
				BeanUtils.copyProperties(form, user);
			}
		}
		return mapping.findForward("edit");
	}

	@SuppressWarnings("unchecked")
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		UserDAO userDao = new UserDAO(User.class);
		User user = (User) userDao.findById(User.class, id);
		userDao.delete(user);

		return mapping.findForward("success");
	}

	public ActionForward info(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		if (id != null && id.trim().length() > 0) {
			UserDAO userDAO = new UserDAO(User.class);
			User user = (User) userDAO.findById(User.class, id);
			request.setAttribute("user", user);
		}
		return mapping.findForward("info");
	}
	/**
	 * 2008-5-28-14:11
	 * 登陆验证,成功后转到主界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward check(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		 ActionErrors errors=form.validate(mapping,request);
		 if(!errors.isEmpty()){
		 saveErrors(request,errors);
		 return edit(mapping,form,request,response);
		 }
		User user=new User();
		BeanUtils.copyProperties(user, form);	
		UserDAO udao=new UserDAO(User.class);
		List list=udao.check(user);				//试验方法不能验证
		Iterator it=list.iterator();
		if(list.size()>0){
			User user1=(User) it.next();
			request.getSession().setAttribute("userId", user1.getId());
			request.getSession().setAttribute("manager", user1.getRole().getRoleName());
		}else{
			System.out.println("=====================无此用户======================");
			return mapping.findForward("failure");}
		return mapping.findForward("success");
	}
}

⌨️ 快捷键说明

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