zoukankan      html  css  js  c++  java
  • MVC案例——修改用户

    修改:

    ——先显示(SELECT操作)修改的页面,在进行修改(update)

    ——显示修改页面

    • Update的超链接:<a href="edit.do?id=<%=customer.getId()%>">
    • edit方法:获取id,调用CustomerDAO的方法获取id对应的Customer对象
    • JSP页面:
      •   获取请求域中的Customer对象,调用对应的字段的get方法来显示值。
      •   使用隐藏域来保存要修改的Customer对象的id:<input type="hidden" name="id" value="<%= customer.getId()%>">
      •        使用隐藏域来保存oldName:<input type="hidden" name="oldName" value="<%= customer.getName()%>">
      •        关于隐藏域:和其他的表单域一样可以被提交到服务器,只不过在页面上不显示
      •        提交到update.do

    步骤:

    1.query.jsp中的修改链接

    2.edit.do

     private void edit(HttpServletRequest req,HttpServletResponse resp){
            String forwardPath="error.jsp";
    
            //1.获取请求参数id
            String idStr = req.getParameter("id");
    
            //2.调用CustomerDAO的customerDAO.get(id)获取和id对应的Customer对象customer
    
            try {
                Customer customer = customerDAO.get(Integer.parseInt(idStr));
                if (customer!=null){
                    forwardPath = "update.jsp";
                    //3.将customer放入request中
                    req.setAttribute("customer",customer);
                }
            }catch (NumberFormatException e){
    
            }
    
            //4.响应updatecustomer.jsp页面:转发
            try {
                req.getRequestDispatcher(forwardPath).forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

      

    3.update.jsp

    <%@ page import="com.mvcapp.entity.Customer" %><%--
      Created by IntelliJ IDEA.
      User: dell
      Date: 2019/7/5
      Time: 15:32
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%
        Object meg = request.getAttribute("message");
        if (meg!=null){
    %>
    <br>
    <b style="color: red"><%=meg%></b>
    <br><br>
    <%
        }
        Integer id= null;
        String oldName = null;
    
        String name = null;
        String password = null;
    
        Customer customer = (Customer) request.getAttribute("customer");
        if (customer!=null){
            id = customer.getId();
            oldName = customer.getName();
            name = customer.getName();
            password = customer.getPassword();
        }else {
            id = Integer.parseInt(request.getParameter("id")) ;
            oldName = request.getParameter("oldName");
            name = request.getParameter("oldName");
            password = request.getParameter("password");
        }
    %>
    
    <form action="update.do" method="post">
        <input type="hidden" name="id" value="<%=id%>">
        <input type="hidden" name="oldName" value=<%= oldName%>>
        <table>
            <tr>
                <td>CustomerName:</td>
                <td><input type="text" name="name"  value="<%=name%>"></td>
            </tr>
            <tr>
                <td>CustomerPassword:</td>
                <td><input type="text" name="password" value="<%=password%>"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="修改" ></td>
            </tr>
        </table>
    </form>
    </body>
    </html>
    

      

    4.update.do

    private void update(HttpServletRequest req, HttpServletResponse resp) {
            //1.获取表单参数:id,name,password
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String oldName = req.getParameter("oldName");
            System.out.println(name);
            System.out.println(password);
            //2.检验name是否已经被占用
            //2.1比较name和oldName是否相同,若相同说明name可用
            if (!oldName.equalsIgnoreCase(name)){
                long count = customerDAO.getCountWithName(name);
                //2.2若返回值大于0,则响应update.jsp页面:通过转发的方式来响应=add.jsp
                if (count>0){
                    //2.2.1在update.jsp页面显示一个错误信息:用户名name已经被占用,请重新选择!
                    //在request中放入一个属性message:用户名name已经被占用,请重新选择,
                    //在页面通过request.getAttribute("message")的方式来显示
                    req.setAttribute("message","用户名"+name+"已经被占用,请重新选择!");
    
                    //2.2.2add.jsp的表单值可以回显,
                    //显示提交表单的新的值,而name显示oldName,而不是新提交的name
                    try {
                        req.getRequestDispatcher("update.jsp").forward(req,resp);
                    } catch (ServletException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    //2.2.3结束方法:return
                    return;
                }
            }
            //3.若验证通过,则把表单参数封装为一个Customer对象customer
            Customer customer = new Customer(name,password);
    
            customer.setId(Integer.parseInt(id));
            //4.调用CustomerDAO的update(Customer customer)执行更新操作
               customerDAO.update(customer);
            //5.重定向到query.do
            try {
                resp.sendRedirect("query.do");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

      

    实验效果:

  • 相关阅读:
    为什么需要Docker?
    一分钟学会《模板方法模式》
    2018再见|2019你好
    三分钟学会《门面模式》
    策略模式原来这么简单!
    外行人都能看得懂的机器学习,错过了血亏!
    我是如何将博客转成PDF的
    面试前必须知道的MySQL命令【explain】
    count(*)、count(1)和count(列名)的区别
    Linux shell去除字符串中所有空格
  • 原文地址:https://www.cnblogs.com/yangHS/p/11139310.html
Copyright © 2011-2022 走看看