增加员工
public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Department> departments=departmentService.getAll(); request.setAttribute("departments",departments); //用于页面的显示,不用管 request.getRequestDispatcher("../staff_add.jsp").forward(request,response); } public void add(HttpServletRequest request, HttpServletResponse response) throws IOException { String account=request.getParameter("account"); String did=request.getParameter("did"); String name=request.getParameter("name"); String sex=request.getParameter("sex"); String idNumber=request.getParameter("idNumber"); Date bornDate=null; try { bornDate=new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("bornDate")); } catch (ParseException e) { e.printStackTrace(); } String info=request.getParameter("info"); Staff staff=new Staff(); staff.setAccount(account); staff.setDid(Integer.parseInt(did)); staff.setName(name); staff.setSex(sex); staff.setIdNumber(idNumber); staff.setBornTime(bornDate); staff.setInfo(info); staffService.add(staff); response.sendRedirect("list.do"); }
删除员工
public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException { String id=request.getParameter("id"); staffService.remove(Integer.parseInt(id)); response.sendRedirect("list.do"); }
要点:new SimpleDateFormat对象:将String类型转换为Date类
编辑(修改)员工信息
public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); Staff staff=staffService.get(Integer.parseInt(id)); List<Department> departments=departmentService.getAll(); request.setAttribute("staff",staff); request.setAttribute("departments",departments); request.getRequestDispatcher("../staff_edit.jsp").forward(request,response); } public void edit(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException { String id=request.getParameter("id"); String account=request.getParameter("account"); String did=request.getParameter("did"); String name=request.getParameter("name"); String sex=request.getParameter("sex"); String idNumber=request.getParameter("idNumber"); String info=request.getParameter("info");
Staff staff=staffService.get(Integer.parseInt(id)); staff.setAccount(account); staff.setDid(Integer.parseInt(did)); staff.setName(name); staff.setSex(sex); staff.setIdNumber(idNumber); Date bornDate=null; try { bornDate=new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("bornDate")); } catch (ParseException e) { e.printStackTrace(); } staff.setBornTime(bornDate); staff.setInfo(info); staffService.edit(staff); response.sendRedirect("list.do"); }
显示员工信息:
public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); Staff staff=staffService.get(Integer.parseInt(id)); request.setAttribute("staff",staff); request.getRequestDispatcher("../staff_detail.jsp").forward(request,response); }