首页 > 其他分享 >删除功能和修改功能

删除功能和修改功能

时间:2022-08-17 09:57:54浏览次数:43  
标签:功能 删除 void request response 修改 user id User

删除功能分析

 

 

 

 

删除功能代码实现

list.jsp:

在forEach遍历中修改:

<a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id});">删除</a>
    <script>
        function deleteUser(id) {
            //用户安全提示
            if (confirm("确定要删除吗?")){
                //访问路径
                location.href = "${pageContext.request.contextPath}/delUserServlet?id="+id;
            }
        }
    </script>

UserDao接口:

void delete(int id);

UserDaoImpl实现类:

    @Override
    public void delete(int id) {
        //1、定义sql
        String sql = "delete from user where id = ?";
        template.update(sql,id);
    }

UserService接口:

/**
     * 根据id删除
     * @param id
     */
    void deleteUser(String id);

UserServiceImpl实现类:

    @Override
    public void deleteUser(String id) {
        dao.delete(Integer.parseInt(id));
    }

DelUserServlet类:

@WebServlet("/delUserServlet")
public class DelUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取id
        String id = request.getParameter("id");
        //2、调用service删除
        UserService service = new UserServiceImpl();
        service.deleteUser(id);

        //3、跳转到查询所有的Servlet
        response.sendRedirect(request.getContextPath()+"/userListServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

 

 

 

 

 

 

修改功能分析

 

 

 

 

 

用户信息回显代码实现

UserDao接口:

User findById(int id);

UserDaoImpl实现类:

@Override
    public User findById(int id) {
        //1、定义sql
        String sql = "select * from user where id = ?";
        User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
        return user;
    }

UserService接口:

/**
     * 根据id删除
     * @param id
     * @return
     */
    User findUserById(String id);

UserServiceImpl实现类:

    @Override
    public User findUserById(String id) {
        return dao.findById(Integer.parseInt(id));
    }

FindUserServlet类:

@WebServlet("/findUserServlet")
public class FindUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取id
        String id = request.getParameter("id");
        //2、调用Service查询
        UserService service = new UserServiceImpl();
        User user = service.findUserById(id);

        //3、将user存入request
        request.setAttribute("user",user);
        //4、转发到update.jsp
        request.getRequestDispatcher("/update.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

update.jsp表单部分:

    <form action="" method="post">
        <div class="form-group">
            <label for="name">姓名:</label>
            <input type="text" class="form-control" id="name" name="name"  value="${user.name}" readonly="readonly" placeholder="请输入姓名" />
        </div>

        <div class="form-group">
            <label>性别:</label>
            <c:if test="${user.gender == '男'}">
                <input type="radio" name="gender" value="男" checked/>男
                <input type="radio" name="gender" value="女"  />女
            </c:if>
            <c:if test="${user.gender == '女'}">
                <input type="radio" name="gender" value="男" />男
                <input type="radio" name="gender" value="女" checked/>女
            </c:if>
        </div>

        <div class="form-group">
            <label for="age">年龄:</label>
            <input type="text" class="form-control" id="age"  name="age" value="${user.age}" placeholder="请输入年龄" />
        </div>

        <div class="form-group">
            <label for="address">籍贯:</label>
            <select name="address" class="form-control" id="address">
                <c:if test="${user.address == '陕西'}">
                    <option value="陕西" selected>陕西</option>
                    <option value="北京">北京</option>
                    <option value="上海">上海</option>
                </c:if>
                <c:if test="${user.address == '北京'}">
                    <option value="陕西">陕西</option>
                    <option value="北京" selected>北京</option>
                    <option value="上海">上海</option>
                </c:if>
                <c:if test="${user.address == '上海'}">
                    <option value="陕西" >陕西</option>
                    <option value="北京">北京</option>
                    <option value="上海" selected>上海</option>
                </c:if>
            </select>
        </div>

        <div class="form-group">
            <label for="qq">QQ:</label>
            <input type="text" class="form-control" id="qq" name="qq" value="${user.qq}" placeholder="请输入QQ号码"/>
        </div>

        <div class="form-group">
            <label for="email">Email:</label>
            <input type="text" class="form-control" id="email" name="email" value="${user.email}" placeholder="请输入邮箱地址"/>
        </div>

        <div class="form-group" style="text-align: center">
            <input class="btn btn-primary" type="submit" value="提交" />
            <input class="btn btn-default" type="reset" value="重置" />
            <input class="btn btn-default" type="button" value="返回"/>
        </div>
    </form>

 

 

 

 

 

用户修改代码实现

UserDao接口:

void update(User user);

UserDaoImpl实现类:

@Override
    public void update(User user) {
        //1、定义sql
        String sql = "update user set name = ?,gender = ?,age = ?,address = ?,qq = ?,email = ? where id = ?";
        template.update(sql,user.getName(),user.getGender(),user.getAge(),user.getAddress(),user.getQq(),user.getEmail(),user.getId());
    }

UserService接口:

    /**
     * 修改用户
     * @param user
     */
    void updateUser(User user);

UserServiceImpl实现类:

@Override
    public void updateUser(User user) {
        dao.update(user);
    }

UpdateUserServlet类:

@WebServlet("/pdateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、设置编码
        request.setCharacterEncoding("utf-8");
        //2、获取map
        Map<String, String[]> map = request.getParameterMap();
        //3、封装对象
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //4、调用service修改
        UserService service = new UserServiceImpl();
        service.updateUser(user);
        //5、跳转到查询所有Servlet
        response.sendRedirect(request.getContextPath()+"/userListServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

 

标签:功能,删除,void,request,response,修改,user,id,User
From: https://www.cnblogs.com/xjw12345/p/16592938.html

相关文章