OA员工管理模块
程序员文章站
2022-06-11 09:53:44
...
OA员工管理模块
要求:
1.查看员工信息、部门、职务、权限。
2.对员工信息进行增删改操作。
3.查看是否是优秀员工。
4.修改当前登录用户信息。
5.根据部门名搜索该部门内员工信息。
6.使用分页
以上功能全部实行,不过还有许多需要改进的地方
如:1、还需使用正则表达式判断用户输入的电话、邮箱等信息;
2、需使用jQuery进行刷新验证用户名是否已使用
3、需使用ajax实现局部刷新、实现二级联动效果
4、删除时应该加判断和友好提示
5、根据部门查询员工时应该使用索引列
6、没有实现意外情况的处理方式
以上需要改进的地方还需要继续学习,如果大家有更好的方法(和学习方法)及见解希望多多分享。。。
实现以上功能其实都是很简单的,不过思维逻辑没理清,没注意细节,就需要花很多时间查错改错,对于初学者的我来说把以上功能模块联合起来做还是有难度,因为我遇到了很多很多问题,过程很崩溃,看似简单可是却花了很多时间实现,这是我第一次因学习上的不会而难过得想哭,正是因为这样,收获也是要比平时多吧,这让我深刻体会到做事一定要先理清条理,再去动手,做什么事都要有原因,其次是一定要非常细心非常细心。还有就是不要气馁,以积极的心态你就又会发现错误所在,解决问题也更有效率。
以下是代码实现过程(有些长有些啰嗦):
dao层代码
package com.oa.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.oa.pojo.Dept; import com.oa.pojo.Emp; import com.oa.pojo.Post; import com.oa.pojo.Purview; import com.oa.util.DBUtil; import com.oa.util.Page; /** * 员工管理模块数据访问层 * @author Administrator * */ public class EmpDao { //建立连接 private Connection conn = null; private PreparedStatement ps = null; private ResultSet rs = null; /** * a.通过部门名称查询员工信息 * @param deptId * @return * @throws SQLException */ public List<Emp> getEmpInfoByDname(String dname) throws SQLException { List<Emp> emps = new ArrayList<Emp>(); conn = DBUtil.getConnection(); String sql = "select e.emp_id, e.emp_name,d.dept_name,p.post_name,e.emp_isexcellent,e.emp_username," + "e.emp_sex,e.emp_phone,e.emp_email,e.emp_address,pu.purview_name from emp_table e,dept_table d," + "post_table p ,purview_table pu where d.dept_name = ? and e.emp_isdelete=0" + " and e.emp_deptid=d.dept_id and e.emp_postid = p.post_id and e.emp_purviewid=pu.purview_id"; try { ps = conn.prepareStatement(sql); ps.setString(1, dname); rs = ps.executeQuery(); while(rs.next()){ Emp emp = new Emp(); Dept dept = new Dept(); Post post = new Post(); Purview purview = new Purview(); emp.setDept(dept); emp.setPost(post); emp.setPurview(purview); emp.setId(rs.getInt("emp_id")); emp.setName(rs.getString("emp_name")); emp.getDept().setName(rs.getString("dept_name")); emp.getPost().setName(rs.getString("post_name")); emp.setIsExcellent(rs.getInt("emp_isexcellent")); emp.setUsername(rs.getString("emp_username")); emp.setSex(rs.getString("emp_sex")); emp.setPhone(rs.getString("emp_phone")); emp.setEmail(rs.getString("emp_email")); emp.setAddress(rs.getString("emp_address")); emp.getPurview().setName(rs.getString("purview_name")); emps.add(emp); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return emps; } /** * b.查询优秀员工 * @param isExcellent * @throws SQLException */ public List<Emp> getExcellent() throws SQLException { List<Emp> emps = new ArrayList<Emp>(); conn = DBUtil.getConnection(); String sql = "select e.emp_id, e.emp_name,d.dept_name,p.post_name,e.emp_isexcellent,e.emp_username," + "e.emp_sex,e.emp_phone,e.emp_email,e.emp_address,pu.purview_name from emp_table e,dept_table d," + "post_table p ,purview_table pu where e.emp_isexcellent = 1 and e.emp_isdelete=0 " + " and e.emp_deptid=d.dept_id and e.emp_postid = p.post_id and e.emp_purviewid=pu.purview_id"; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ Emp emp = new Emp(); Dept dept = new Dept(); Post post = new Post(); Purview purview = new Purview(); emp.setDept(dept); emp.setPost(post); emp.setId(rs.getInt("emp_id")); emp.setPurview(purview); emp.setName(rs.getString("emp_name")); emp.getDept().setName(rs.getString("dept_name")); emp.getPost().setName(rs.getString("post_name")); emp.setIsExcellent(rs.getInt("emp_isexcellent")); emp.setUsername(rs.getString("emp_username")); emp.setSex(rs.getString("emp_sex")); emp.setPhone(rs.getString("emp_phone")); emp.setEmail(rs.getString("emp_email")); emp.setAddress(rs.getString("emp_address")); emp.getPurview().setName(rs.getString("purview_name")); emps.add(emp); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return emps; } /** * c. 显示员工详情 * @throws SQLException */ public List<Emp> getEmpInfos() throws SQLException { List<Emp> emps = new ArrayList<Emp>(); conn = DBUtil.getConnection(); String sql = "select e.emp_id, e.emp_name,d.dept_name,p.post_name,e.emp_isexcellent,e.emp_username," + "e.emp_sex,e.emp_phone,e.emp_email,e.emp_address,pu.purview_name from emp_table e,dept_table d," + "post_table p ,purview_table pu where e.emp_isdelete=0 and" + " e.emp_deptid=d.dept_id and e.emp_postid = p.post_id and e.emp_purviewid=pu.purview_id"; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ Emp emp = new Emp(); Dept dept = new Dept(); Post post = new Post(); Purview purview = new Purview(); emp.setDept(dept); emp.setPost(post); emp.setPurview(purview); emp.setId(rs.getInt("emp_id")); emp.setName(rs.getString("emp_name")); emp.getDept().setName(rs.getString("dept_name")); emp.getPost().setName(rs.getString("post_name")); emp.setIsExcellent(rs.getInt("emp_isexcellent")); emp.setUsername(rs.getString("emp_username")); emp.setSex(rs.getString("emp_sex")); emp.setPhone(rs.getString("emp_phone")); emp.setEmail(rs.getString("emp_email")); emp.setAddress(rs.getString("emp_address")); emp.getPurview().setName(rs.getString("purview_name")); emps.add(emp); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return emps; } /** * d.修改员工信息 * @param emp * @throws SQLException */ public boolean updateEmpInfos(Emp emp) throws SQLException { boolean result = false; conn = DBUtil.getConnection(); String sql = "update Emp_Table set Emp_deptid=?,Emp_postid=?," + "Emp_isexcellent=?," + "Emp_name=?,Emp_sex=?," + "Emp_phone=?,Emp_email=?,Emp_address=?," + "emp_PurviewId=? where Emp_id=? "; try { ps = conn.prepareStatement(sql); ps.setInt(1, emp.getDept().getId()); ps.setInt(2, emp.getPost().getId()); ps.setInt(3, emp.getIsExcellent()); ps.setString(4, emp.getName()); ps.setString(5, emp.getSex()); ps.setString(6, emp.getPhone()); ps.setString(7, emp.getEmail()); ps.setString(8, emp.getAddress()); ps.setInt(9, emp.getPurview().getId()); ps.setInt(10,emp.getId()); int count = ps.executeUpdate(); if(count>0){ result = true; } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return result; } /** * e.删除员工信息 * @param id * @throws SQLException */ public boolean deleteEmpInfo(int id) throws SQLException { boolean result = false; conn = DBUtil.getConnection(); String sql = "delete from Emp_Table where Emp_id = ? "; try { ps = conn.prepareStatement(sql); ps.setInt(1, id); int count = ps.executeUpdate(); if(count>0){ result = true; } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return result; } /** * f.修改当前登录用户信息 * @return * @throws SQLException */ public boolean updateNowUserInfor(Emp emp) throws SQLException { boolean result = false; conn = DBUtil.getConnection(); String sql ="update Emp_Table set Emp_deptid=?,Emp_postid=?,emp_purviewid=?,Emp_isexcellent=?," + "Emp_name=?,Emp_sex=?,Emp_phone=?,Emp_email=?,Emp_address=?" + "where Emp_username=?"; try { ps = conn.prepareStatement(sql); ps.setInt(1, emp.getDept().getId()); ps.setInt(2, emp.getPost().getId()); ps.setInt(3, emp.getPurview().getId()); ps.setInt(4, emp.getIsExcellent()); ps.setString(5,emp.getName()); ps.setString(6, emp.getSex()); ps.setString(7, emp.getPhone()); ps.setString(8, emp.getEmail()); ps.setString(9, emp.getAddress()); ps.setString(10, emp.getUsername()); int count = ps.executeUpdate(); if(count>0){ result = true; } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return result ; } /** * 分页显示 * @param currentPage * @param rows * @return * @throws SQLException */ public Page<Emp> getEmpByPage(int currentPage, int rows) throws SQLException { Page<Emp> page = new Page<Emp>(); List<Emp> emps = new ArrayList<Emp>(); conn = DBUtil.getConnection(); String sql = "select e.emp_id,e.emp_name,d.dept_name,p.post_name,pu.purview_name,e.emp_isexcellent," + "e.emp_username,e.emp_sex,e.emp_phone,e.emp_email,e.emp_address from(select emp_id, emp_isdelete," + "Emp_isexcellent,Emp_username,Emp_name,Emp_sex,Emp_phone,Emp_email,emp_deptid,emp_postid,Emp_address ," + "emp_purviewid,rownum r from emp_table )e,dept_table d ,post_table p,purview_table pu " + "where e.r >? and e.r <=? and e.emp_deptid=d.dept_id and e.emp_postid = p.post_id and" + " e.emp_purviewid=pu.purview_id and e.emp_isdelete=0 "; int begin = (currentPage -1)*rows; int end = currentPage*rows; try { ps = conn.prepareStatement(sql); ps.setInt(1, begin); ps.setInt(2, end); rs = ps.executeQuery(); while(rs.next()){ Emp emp = new Emp(); Dept dept = new Dept(); Post post = new Post(); Purview purview = new Purview(); emp.setDept(dept); emp.setPost(post); emp.setPurview(purview); emp.setId(rs.getInt("emp_id")); emp.setName(rs.getString("emp_name")); emp.getDept().setName(rs.getString("dept_name")); emp.getPost().setName(rs.getString("post_name")); emp.setIsExcellent(rs.getInt("emp_isexcellent")); emp.setUsername(rs.getString("emp_username")); emp.setSex(rs.getString("emp_sex")); emp.setPhone(rs.getString("emp_phone")); emp.setEmail(rs.getString("emp_email")); emp.setAddress(rs.getString("emp_address")); emp.getPurview().setName(rs.getString("purview_name")); emps.add(emp); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } page.setData(emps); page.setCurrentPage(currentPage); page.setRecords(getRecords()); page.setRows(rows); page.getPageCount(); return page; } /** * 获取总记录数 * @return * @throws SQLException */ public int getRecords() throws SQLException{ int result = 0; conn = DBUtil.getConnection(); String sql = "select count(emp_id) from emp_table where emp_isdelete=0 "; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); if (rs.next()) { result = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return result; } /** * 1、添加员工信息 * @return * @throws SQLException */ public boolean addEmpInfos(Emp emp) throws SQLException { boolean result = false; conn = DBUtil.getConnection(); String sql = "insert into Emp_Table values(seq_emps.nextval,?,?,0,?,?,?,?,?,?,?,?,?) "; try { ps = conn.prepareStatement(sql); ps.setInt(1, emp.getDept().getId()); ps.setInt(2, emp.getPost().getId()); ps.setInt(3, emp.getIsExcellent()); ps.setString(4, emp.getUsername()); ps.setString(5, emp.getPassword()); ps.setString(6, emp.getName()); ps.setString(7, emp.getSex()); ps.setString(8, emp.getPhone()); ps.setString(9, emp.getEmail()); ps.setString(10, emp.getAddress()); ps.setInt(11, emp.getPurview().getId()); int count = ps.executeUpdate(); if(count>0){ result = true; } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return result; } /** * 获得所有的职位 * @return 返回所有职位的集合 * @throws SQLException */ public List<Post> getPostList() throws SQLException{ List<Post> postList = new ArrayList<Post>(); Post post = new Post(); conn = DBUtil.getConnection(); String sql = "select post_id,post_name,post_deptid from post_table "; // String sql = "select p.Post_Id,p.Post_Name,p.post_deptid from Post_Table p,dept_table d where d.dept_id = p.post_deptid "; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ Dept dept = new Dept(); post.setDept(dept); post.setId(rs.getInt("Post_Id")); post.setName(rs.getString("Post_Name")); post.getDept().setId(rs.getInt("post_deptid")); postList.add(post); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return postList; } /** * 得到所有部门的集合 * @return 返回所有部门的集合 * @throws SQLException */ public List<Dept> getDeptList() throws SQLException{ List<Dept> deptList = new ArrayList<Dept>(); conn = DBUtil.getConnection(); String sql = "select Dept_Id,Dept_Name from Dept_Table "; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ Dept dept = new Dept(); dept.setId(rs.getInt("Dept_Id")); dept.setName(rs.getString("Dept_Name")); deptList.add(dept); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return deptList; } /** * 获得所有权限的集合 * @return * @throws SQLException */ public List<Purview> getPurviewList() throws SQLException{ List<Purview> purviewList = new ArrayList<Purview>(); conn = DBUtil.getConnection(); String sql = "select Purview_Id,Purview_Name from Purview_Table "; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ Purview p = new Purview(); p.setId(rs.getInt("Purview_Id")); p.setName(rs.getString("Purview_Name")); purviewList.add(p); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return purviewList; } public Emp getEmpByName(String username) throws SQLException{ Emp emp = new Emp(); conn =DBUtil.getConnection(); String sql="select e.emp_name,d.dept_id,d.dept_name,p.post_id,p.post_name,e.emp_isexcellent,e.emp_username,e.emp_password," + "e.emp_sex,e.emp_phone,e.emp_email,e.emp_address,e.emp_purviewId ,pu.purview_name from emp_table e,dept_table d," + "post_table p ,purview_table pu where e.emp_username = ? and e.emp_isdelete=0 " + " and e.emp_deptid=d.dept_id and e.emp_postid = p.post_id and e.emp_purviewid=pu.purview_id"; try { ps = conn.prepareStatement(sql); ps.setString(1, username); rs = ps.executeQuery(); if(rs.next()){ Dept dept = new Dept(); Post post = new Post(); Purview purview = new Purview(); emp.setDept(dept); emp.setPost(post); emp.setPurview(purview); emp.setName(rs.getString("emp_name")); emp.getDept().setId(rs.getInt("dept_id")); emp.getDept().setName(rs.getString("dept_name")); emp.getPost().setId(rs.getInt("post_id")); emp.getPurview().setId(rs.getInt("purview_id")); emp.getPost().setName(rs.getString("post_name")); emp.setIsExcellent(rs.getInt("emp_isexcellent")); emp.setUsername(username); emp.setSex(rs.getString("emp_sex")); emp.setPhone(rs.getString("emp_phone")); emp.setEmail(rs.getString("emp_email")); emp.setAddress(rs.getString("emp_address")); emp.setPassword(rs.getString("emp_password")); emp.getPurview().setName(rs.getString("purview_name")); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return emp; } /** * 通过员工id查找员工信息 * @param username * @return * @throws SQLException */ public Emp getEmpById(int id) throws SQLException { Emp emp = new Emp(); conn = DBUtil.getConnection(); String sql = "select e.emp_name,d.dept_id,d.dept_name,p.post_id,p.post_name,e.emp_isexcellent,e.emp_username," + "e.emp_sex,e.emp_phone,e.emp_email,e.emp_address,e.emp_purviewId ,pu.purview_name from emp_table e,dept_table d," + "post_table p ,purview_table pu where e.emp_id = ? and e.emp_isdelete=0 " + " and e.emp_deptid=d.dept_id and e.emp_postid = p.post_id and e.emp_purviewid=pu.purview_id"; try { ps = conn.prepareStatement(sql); ps.setInt(1, id); rs = ps.executeQuery(); if(rs.next()){ Dept dept = new Dept(); Post post = new Post(); Purview purview = new Purview(); emp.setDept(dept); emp.setPost(post); emp.setPurview(purview); emp.setId(id); emp.setName(rs.getString("emp_name")); emp.getDept().setId(rs.getInt("dept_id")); emp.getDept().setName(rs.getString("dept_name")); emp.getPost().setId(rs.getInt("post_id")); emp.getPost().setName(rs.getString("post_name")); emp.setIsExcellent(rs.getInt("emp_isexcellent")); emp.setUsername(rs.getString("emp_username")); emp.setSex(rs.getString("emp_sex")); emp.setPhone(rs.getString("emp_phone")); emp.setEmail(rs.getString("emp_email")); emp.setAddress(rs.getString("emp_address")); emp.getPurview().setId(rs.getInt("emp_purviewId")); emp.getPurview().setName(rs.getString("purview_name")); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return emp; } /** * 通过用户名查找员工信息 * @param username * @return * @throws SQLException */ public Emp getEmpByUserName(String username) throws SQLException { Emp emp = new Emp(); conn = DBUtil.getConnection(); String sql = "select e.emp_name,d.dept_id,d.dept_name,p.post_id,p.post_name,e.emp_isexcellent,e.emp_username," + "e.emp_sex,e.emp_phone,e.emp_email,e.emp_address,e.emp_purviewId ,pu.purview_name from emp_table e,dept_table d," + "post_table p ,purview_table pu where e.emp_username = ? and e.emp_isdelete=0 " + " and e.emp_deptid=d.dept_id and e.emp_postid = p.post_id and e.emp_purviewid=pu.purview_id"; try { ps = conn.prepareStatement(sql); ps.setString(1, username); rs = ps.executeQuery(); if(rs.next()){ Dept dept = new Dept(); Post post = new Post(); Purview purview = new Purview(); emp.setDept(dept); emp.setPost(post); emp.setPurview(purview); emp.setUsername(username); emp.setName(rs.getString("emp_name")); emp.getDept().setId(rs.getInt("dept_id")); emp.getDept().setName(rs.getString("dept_name")); emp.getPost().setId(rs.getInt("post_id")); emp.getPost().setName(rs.getString("post_name")); emp.setIsExcellent(rs.getInt("emp_isexcellent")); emp.setSex(rs.getString("emp_sex")); emp.setPhone(rs.getString("emp_phone")); emp.setEmail(rs.getString("emp_email")); emp.setAddress(rs.getString("emp_address")); emp.getPurview().setId(rs.getInt("emp_purviewId")); emp.getPurview().setName(rs.getString("purview_name")); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs, ps, conn); } return emp; } }
sevlet层
package com.oa.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.oa.pojo.Dept; import com.oa.pojo.Emp; import com.oa.pojo.Post; import com.oa.pojo.Purview; import com.oa.service.EmpService; import com.oa.util.Page; /** * 员工管理模块控制器 * @author Administrator */ @SuppressWarnings("serial") public class EmpServlet extends HttpServlet { private EmpService empService = new EmpService(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取用户提交过来的操作类型 String type = request.getParameter("type"); //d.修改员工信息(大致流程:从页面点击修改按钮以get方法提交,在此方法中跳转到修改页面,从修改页面点击保存以post方式提交,到post方法里找到匹配的修改方法后把从页面获取的数据添加进数据库再显示出来) if("updateEmp".equals(type)){ int id =Integer.parseInt( request.getParameter("id")); Emp emp = empService.getEmpById(id); List<Dept> deptList = empService.getDeptList(); List<Post> postList = empService.getPostList(); List<Purview> purviewList = empService.getPurviewList(); request.setAttribute("emp",emp); request.setAttribute("deptList",deptList); request.setAttribute("postList",postList); request.setAttribute("purviewList",purviewList); request.getRequestDispatcher("updateemp.jsp").forward(request, response); } //添加员工、实现流程与删除员工相似 else if ("addEmp".equals(type)) { List<Dept> deptList = empService.getDeptList(); List<Post> postList = empService.getPostList(); List<Purview> purviewList = empService.getPurviewList(); request.setAttribute("deptList",deptList); request.setAttribute("postList",postList); request.setAttribute("purviewList",purviewList); request.getRequestDispatcher("addemp.jsp").forward(request, response); } //a. 按‘部门’搜索员工 else if("deptName".equals(type)){ String type2 = request.getParameter("deptName"); List<Emp> emps = empService.getEmpInfoByDname(type2); request.setAttribute("emps",emps); request.getRequestDispatcher("searchemp.jsp").forward(request, response); } //b. 显示优秀员工 else if("getExcellent".equals(type)){ List<Emp> emps= empService.getExcellent(); request.setAttribute("emps", emps); request.getRequestDispatcher("excellent.jsp").forward(request, response); } //f.修改当前登录用户信息 else if("updateNowUserInfor".equals(type)){ List<Dept> deptList = empService.getDeptList(); List<Post> postList = empService.getPostList(); List<Purview> purviewList = empService.getPurviewList(); request.setAttribute("deptList",deptList); request.setAttribute("postList",postList); request.setAttribute("purviewList",purviewList); HttpSession session = request.getSession(); Emp emp1 = (Emp) session.getAttribute("emp"); String username = emp1.getUsername(); Emp emp = empService.getEmpByUserName(username); request.setAttribute("emp",emp); request.getRequestDispatcher("updatenowuser.jsp").forward(request, response); } //显示员工详情 else{ getAllEmps(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取用户提交过来的操作类型 String type = request.getParameter("type"); //2.a 按‘部门名称’搜索员工 if("deptName".equals(type)){ String type2 = request.getParameter("deptName"); List<Emp> emps = empService.getEmpInfoByDname(type2); request.setAttribute("emps",emps); request.getRequestDispatcher("searchemp.jsp").forward(request, response); } //2.b 优秀员工 else if("getExcellent".equals(type)){ List<Emp> emps= empService.getExcellent(); request.setAttribute("emps", emps); request.getRequestDispatcher("excellent.jsp").forward(request, response); } //2.d 修改员工信息 else if("updateEmp".equals(type)){ Emp emp = getFromEmpUp(request); empService.updateEmpInfos(emp); List<Emp> emps =empService.getEmpInfos(); request.setAttribute("emps", emps); getAllEmps(request, response); } //2.f 修改当前登录用户信息 else if("updateNowUserInfor".equals(type)){ Emp emp = getFromEmpUpUser(request); empService.updateNowUserInfor(emp); List<Emp> emps =empService.getEmpInfos(); request.setAttribute("emps", emps); getAllEmps(request, response); } else if ("deleteEmp".equals(type)){ int id = Integer.parseInt(request.getParameter("id")); empService.deleteEmpInfo(id); getAllEmps(request, response); } else if ("addEmp".equals(type)) { Emp emp = getFromEmpAd(request); empService.addEmpInfos(emp); List<Emp> emps =empService.getEmpInfos(); request.setAttribute("emps", emps); getAllEmps(request, response); } //2.c 显示员工详情 else{ getAllEmps(request, response); } } /*** * 实现分页功能的查询 */ public void getAllEmps(HttpServletRequest request,HttpServletResponse response) { //用户传入的当前页面 int currentPage = null == request.getParameter("currentPage")?1:Integer.parseInt(request.getParameter("currentPage")); //用户传入的页面显示的行数,初始化行数自定义 int rows = null == request.getParameter("rows")?3:Integer.parseInt(request.getParameter("rows")); Page<Emp> page = empService.getEmpByPage(currentPage,rows); request.setAttribute("page", page); try { request.getRequestDispatcher("emp.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); } } /** * 封装从页面接收的修改信息 * @param request * @param response * @return */ public Emp getFromEmpUp(HttpServletRequest request) { Emp emp = new Emp(); Dept dept = new Dept(); emp.setDept(dept); Post post = new Post(); emp.setPost(post); Purview purview = new Purview(); emp.setPurview(purview); emp.setId(Integer.parseInt(request.getParameter("id"))); emp.getDept().setId(Integer.parseInt(request.getParameter("deptId"))); emp.getPost().setId(Integer.parseInt(request.getParameter("postId"))); emp.setIsExcellent(Integer.parseInt(request.getParameter("isExcellent"))); emp.setName(request.getParameter("name")); emp.setSex(request.getParameter("sex")); emp.setPhone(request.getParameter("phone")); emp.setEmail(request.getParameter("email")); emp.setAddress(request.getParameter("address")); emp.getPurview().setId(Integer.parseInt(request.getParameter("purviewId"))); return emp; } /** * 封装从页面获取的修改当前用户的信息 * @param request * @return */ public Emp getFromEmpUpUser(HttpServletRequest request) { Emp sessionEmp = (Emp)request.getSession().getAttribute("emp"); String username = sessionEmp.getUsername(); Emp emp = new Emp(); Dept dept = new Dept(); emp.setDept(dept); Post post = new Post(); emp.setPost(post); Purview purview = new Purview(); emp.setPurview(purview); emp.setId(Integer.parseInt(request.getParameter("id"))); emp.getDept().setId(Integer.parseInt(request.getParameter("deptId"))); emp.getPost().setId(Integer.parseInt(request.getParameter("postId"))); emp.getDept().setName(request.getParameter("deptname")); emp.getPost().setName(request.getParameter("postname")); emp.setIsExcellent(Integer.parseInt(request.getParameter("isExcellent"))); emp.setUsername(username); emp.setName(request.getParameter("name")); emp.setSex(request.getParameter("sex")); emp.setPhone(request.getParameter("phone")); emp.setEmail(request.getParameter("email")); emp.setAddress(request.getParameter("address")); emp.getPurview().setId(Integer.parseInt(request.getParameter("purviewId"))); return emp; } /** * 封装从页面接收的新增信息 * @param request * @param response * @return */ public Emp getFromEmpAd(HttpServletRequest request) { Emp emp = new Emp(); Dept dept = new Dept(); emp.setDept(dept); Post post = new Post(); emp.setPost(post); Purview purview = new Purview(); emp.setPurview(purview); emp.getDept().setId(Integer.parseInt(request.getParameter("deptId"))); emp.getPost().setId(Integer.parseInt(request.getParameter("postId"))); emp.setIsExcellent(Integer.parseInt(request.getParameter("isExcellent"))); emp.setUsername(request.getParameter("username")); emp.setPassword(request.getParameter("password")); emp.setName(request.getParameter("name")); emp.setSex(request.getParameter("sex")); emp.setPhone(request.getParameter("phone")); emp.setEmail(request.getParameter("email")); emp.setAddress(request.getParameter("address")); emp.getPurview().setId(Integer.parseInt(request.getParameter("purviewId"))); return emp; }
util包中的page代码
package com.oa.util; import java.util.ArrayList; import java.util.List; public class Page<T> { // 存储当前页面的值 private int currentPage; // 页面显示数据的行数 private int rows; // 数据库中的总记录数 private int records; // 页面的总页数 private int pageCount; // 存放数据的集合 private List<T> data = new ArrayList<T>(); public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public int getRecords() { return records; } public void setRecords(int records) { this.records = records; } /** * 计算总页数 * @return */ public int getPageCount() { if (records % rows == 0) { pageCount = records / rows; } else { pageCount = records / rows + 1; } return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } }
员工详细信息和分页的页面代码
<h5>员工详细信息</h5> <form id="search" action="EmpServlet" method="post"> <div style="float: right;"> 输入部门名称:<input type="text" name="deptName" id="deptName" /> <input type="hidden" name="type" value="deptName" /> <input type="submit" value="搜索" id="searchEmp"/> </div> </form> </div> <div class="widget-content nopadding"> <table class="table table-bordered table-striped"> <thead> <tr> <th>姓名</th> <th>部门名</th> <th>职位名</th> <th>是否优秀员工</th> <th>用户名</th> <th>性别</th> <th>电话</th> <th>邮箱</th> <th>地址</th> <th>权限名</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${page.data}" var="emp"> <tr class="odd gradeX"> <td>${emp.name }</td> <td>${emp.dept.name }</td> <td>${emp.post.name }</td> <td>${emp.isExcellent }</td> <td>${emp.username }</td> <td>${emp.sex }</td> <td>${emp.phone }</td> <td>${emp.email }</td> <td>${emp.address }</td> <td>${emp.purview.name }</td> <td>[url=EmpServlet?type=updateEmp&id=${emp.id }]修改[/url] [url=EmpServlet?type=deleteEmp&id=${emp.id }]删除[/url]</td> </tr> </c:forEach> </tbody> </table> <div class="pagination" style="float: right;"> [list] [*][url=EmpServlet?type=addEmp]新增[/url] <li> [url=EmpServlet?type=getExcellent]查询优秀员[/url] </li> [*][url=EmpServlet?type=updateNowUserInfor]修改当前用户信息[/url] [*][url=#]当前页面:${page.currentPage }[/url] <li class="active">[url=EmpServlet?currentPage=1]首页[/url]</li> <li> <c:if test="${page.currentPage-1<=0 }"> [url=EmpServlet?currentPage=1]上一页[/url] </c:if> <c:if test="${page.currentPage-1>0 }"> [url=EmpServlet?currentPage=${page.currentPage-1 }]上一页[/url] </c:if> </li> <li> <c:if test="${page.currentPage+1>page.pageCount }"> [url=EmpServlet?currentPage=${page.pageCount }]下一页[/url] </c:if> <c:if test="${page.currentPage+1<=page.pageCount }"> [url=EmpServlet?currentPage=${page.currentPage+1 }]下一页[/url] </c:if> </li> <li><a href="EmpServlet?currentPage=${page.pageCount }">末页</a></li> [*]共:${page.pageCount } 页 <li><input type="number" id="currentPage" /> <input type="button" value="go" onclick="goPage(${page.pageCount })" /> </li> [/list] </div> </div>
分页函数
function goPage(pageCount) { //跳转到用户输入的页面 var currentPage = document.getElementById("currentPage").value; if (currentPage <= 0) { currentPage = 1; } if (currentPage > pageCount) { currentPage = pageCount; } window.location.href="EmpServlet?currentPage="+currentPage; }
连接TomCat后在本地测试结果截图:
1、以超级管理员身份登录——用户名:jack 密码:a123456,如当前登录用户图所示:
[img]
[/img]
2、登录成功后进入主页,在主页点击员工管理,出现员工详细信息,如员工详情图所示:
[img]
[/img]
3、在“输入部门名称”搜索框中输入财务部,出现财务部员工详情:
[img]
[/img]
[img]
[/img]
4、点击修改可修改员工信息,其修改内容没有当前登录用户可修改权限大
[img]
[/img]
5、点击修改当前用户信息便可修改当前用户的密码:
[img][/img]
6、修改员工和修改当前用户信息成功后的结果图:
[img]
[/img]
7、点击优秀员工按钮可查询优秀员工信息:
[img]
[/img]
8、点击新增,可新增员工:
[img]
[/img]
[img]
[/img]
9、删除员工,删除员工在数据库里的其实是将0置1,并没有从数据库里删除(由于没写提示所以截图看不出是否删除成功,故将数据删除到只剩一页显示):
[img]
[/img]
上一篇: 微信怎么与朋友分享音乐歌曲视频