SSSP-CRUD-删除

来源:互联网 发布:乐语客服软件 编辑:程序博客网 时间:2024/06/06 08:51

确定开发目标:
URL:emp/{id}、method:DELETE
Dao 层:直接使用 SpringData 已经自带的 delete 方法即可
Service 层:直接调用 Dao 方法即可
Controller 层:

  • 直接调用 Service 方法 重定向到 /emps

JSP 页面:

  • 使用 JS confirm:确定要删除 xx 的信息吗,把超链接的 GET 请求转为 POST 请求,且携带_method=DELETE 的请求参数

emp下的list.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript" src="${pageContext.request.contextPath }/scripts/jquery-1.9.1.min.js"></script><script type="text/javascript">    $(function(){        $(".delete").click(function(){            var label = $(this).next(":hidden").val();            var flag = confirm("确定要删除" + label + "的信息吗?");            if(flag){                var url = $(this).attr("href");                $("#_form").attr("action", url);                $("#_method").val("DELETE");                $("#_form").submit();            }            return false;        });    })</script></head><body>    <form action="" method="POST" id="_form">        <input type="hidden" id="_method" name="_method"/>    </form>    <c:if test="${page == null || page.numberOfElements == 0 }">        没有任何记录.     </c:if>    <c:if test="${page != null && page.numberOfElements > 0 }">        <table border="1" cellpadding="10" cellspacing="0">            <tr>                <th>ID</th>                <th>LastName</th>                <th>Email</th>                <th>Birth</th>                <th>CreateTime</th>                <th>Department</th>                <th>Edit</th>                <th>Delete</th>            </tr>            <c:forEach items="${page.content }" var="emp">                <tr>                    <td>${emp.id }</td>                    <td>${emp.lastName }</td>                    <td>${emp.email }</td>                    <td>                        <fmt:formatDate value="${emp.birth }" pattern="yyyy-MM-dd"/>                    </td>                    <td>                        <fmt:formatDate value="${emp.createTime }" pattern="yyyy-MM-dd hh:mm:ss"/>                    </td>                    <td>${emp.department.departmentName }</td>                    <td><a href="${pageContext.request.contextPath }/emp/${emp.id}">Edit</a></td>                    <td>                        <a href="${pageContext.request.contextPath }/emp/${emp.id}" class="delete">Delete</a>                        <input type="hidden" value="${emp.lastName }"/>                    </td>                </tr>            </c:forEach>            <tr>                <td colspan="8">                    共 ${page.totalElements } 条记录                    共 ${page.totalPages } 页                    当前 ${page.number + 1 } 页                    <a href="?pageNo=${page.number + 1 - 1 }">上一页</a>                    <a href="?pageNo=${page.number + 1 + 1 }">下一页</a>                </td>            </tr>        </table>    </c:if></body></html>

EmployeeService.java:

@Transactionalpublic void delete(Integer id){    employeeRepository.delete(id);}

EmployeeHandler.java:

@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)public String delete(@PathVariable("id") Integer id){    employeeService.delete(id);    return "redirect:/emps";}
原创粉丝点击