javaweb员工表
-
首先把员工表的信息查询显示出来
@WebServlet( "/SelectServlet") public class SelectServlet extends ViewBaseServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Emp emp=new Emp(); EmpDao empDao=new EmpDao(); //查询数据存储到列表中 List<Emp> list = empDao.selectEmp(); request.setAttribute("list",list); super.processTemplate("select",request,response); } } <body> <table> <tr> <th>工号</th> <th>姓名</th> <th>入职时间</th> <th>薪水</th> <th>功能</th> </tr> <tr th:each="m:${list}"> <td th:text="${m.getId}"></td> <td th:text="${m.getEname}"></td> <td th:text="${m.getJoindate}"></td> <td th:text="${m.getSalary}"></td> <td> //通过thymeleaf传递用户id <a th:href="@{/ToEditServlet(id=${m.id})}">编辑</a> <a th:href="@{/DelServlet(id=${m.id})}">删除</a> </td> </tr> </table> <input type="button" value="添加员工" οnclick="add()"> <input type="button" value="删除员工" οnclick="del()"> <script> function add(){ location.href="ToAdddServlet"; } function del(){ location.href="ToDelServlet"; } </script>
-
实现添加用户功能
//首先需要通过渲染add.html,实现动态提交路径 @WebServlet(value = "/ToAdddServlet") public class ToAdddServlet extends ViewBaseServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.processTemplate("add", request, response); } } @WebServlet("/AddServlet") public class AddServlet extends ViewBaseServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置接收中文编码格式 request.setCharacterEncoding("utf-8"); String ename = request.getParameter("ename"); String joindate = request.getParameter("joindate"); String salary = request.getParameter("salary"); Emp emp=new Emp(); emp.setEname(ename); emp.setSalary(salary); emp.setJoindate(joindate); EmpDao empDao=new EmpDao(); int i = empDao.insertEmp(emp); //当i>0时,添加用户数据成功 if(i>0){ response.sendRedirect("SelectServlet"); System.out.println("insert success"); } } } <form th:action="@{/AddServlet}" method="post"> <table> <tr><td>员工名字:</td> <td><input type="text" name="ename"></td> </tr> <tr><td>入职时间:</td> <td><input type="text" name="joindate"></td> </tr> <tr><td>员工薪水:</td> <td><input type="text" name="salary"></td> </tr> </table> <input type="submit" value="提交"> </form>
-
实现用户数据编辑功能(直接在首页用户信息旁边),首先需要检验编辑按钮是否传来了id
@WebServlet(value = "/ToEditServlet") public class ToEditServlet extends ViewBaseServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); //接收页面传来的id,没有则无法进行编辑 if(!StringUtils.isEmpty(id)){ Integer i = Integer.valueOf(id); EmpDao empDao=new EmpDao(); Emp emp = empDao.selectId(i); request.setAttribute("emp",emp); } super.processTemplate("edit", request, response); } } <form th:action="@{/update}" method="post"> <h2>修改员工</h2> <input type="hidden" th:value="${emp.id}" name="id"> 用户名:<input type="text" th:value="${emp.ename}" name="ename"><br> 入职时间:<input type="text" th:value="${emp.joindate}" name="joindate"><br> 薪水:<input type="text" th:value="${emp.salary}" name="salary"><br> <input type="submit" value="修改数据"> </form> @WebServlet("/update") public class EditServlet extends ViewBaseServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置修改中文不乱码 request.setCharacterEncoding("utf-8"); //把网页上修改的数据存到emp中 int id =Integer.valueOf( request.getParameter("id")); String ename = request.getParameter("ename"); String joindate = request.getParameter("joindate"); String salary = request.getParameter("salary"); Emp emp=new Emp(); emp.setEname(ename); emp.setId(id); emp.setJoindate(joindate); emp.setSalary(salary); EmpDao empDao=new EmpDao(); //实现emp数据的更新 int i = empDao.updateEmp(emp); if(i>0){ //跳转到查询页面 response.sendRedirect("SelectServlet"); } } }
-
实现删除功能,直接传id
@WebServlet(value = "/ToDelServlet") public class ToDelServlet extends ViewBaseServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.processTemplate("del", request, response); } } <form th:action="@{/DelServlet}" method="post"> 请输入要删除的员工id:<input type="text" name="id"> <input type="submit" value="提交"> </form> @WebServlet(value = "/DelServlet") public class DelServlet extends ViewBaseServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id =Integer.valueOf( request.getParameter("id")); Emp emp=new Emp(); EmpDao empDao=new EmpDao(); emp.setId(id); int i = empDao.delEmp(emp); //删除成功后跳转到首页 if(i>0){ response.sendRedirect("SelectServlet"); }else { System.out.println("删除失败"); } } }
总结:
-
在实现功能跳转的时候,可以设置动态路径,这样就需要在两者中间加一个渲染,直接渲染到页面
-
在实现编辑和删除的功能时,可以直接在跳转路径后面加上id,这样就可以通过id来直接操作
-
注意设置好网页中输入的中文编码格式
-
注意html文件放置的路径(一般在web根目录里面),还有数据库配置文件的路径在src根目录下
-
-