SpringMVC----RESTFUL_CRUD_删除操作&处理静态资源

1.

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"%>
<!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>
<!-- SpringMVC处理静态资源
    1.为什么会有这种问题?
        优雅的REST风格的资源URL不希望带.html或.do等后缀;
        若将DispatcherServlet请求映射为/,则springMVC将捕获WEB容器的所有请求,包括静态资源(图片、css、js)的请求,springMVC会将
        他们当成一个普通请求处理,因此找不到对应的处理器将导致错误
    2.如何解决:
        在springMVC的配置文件中配置:<mvc:default-servlet-handler/>的方式解决静态资源的问题
 -->
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(function() {
        $(".delete").click(function() {
            var href = $(this).attr("href");
            $("form").attr("action", href).submit();
            return false;
        });
    })
</script>
</head>
<body>

    <form action="" method="POST">
        <input type="hidden" name="_method" value="DELETE" />
    </form>


    <c:if test="${empty requestScope.employees }">
        没有任何员工信息.
    </c:if>
    <c:if test="${!empty requestScope.employees }">
        <table border="1" cellpadding="10" cellspacing="0">
            <tr>
                <th>ID</th>
                <th>LastName</th>
                <th>Email</th>
                <th>Gender</th>
                <th>Department</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
            <c:forEach items="${requestScope.employees }" var="emp">
                <tr>
                    <td>${emp.id }</td>
                    <td>${emp.lastName }</td>
                    <td>${emp.email }</td>
                    <td>${emp.gender == 0 ? 'Female' : 'Male' }</td>
                    <td>${emp.department.departmentName }</td>
                    <td><a href="#">Edit</a></td>
                    <!-- 默认超链接是一个GET请求,但是使用HiddenHttpMethodFilter后POST请求才能转换成DELETE请求(借助js实现) -->
                    <td><a class="delete" href="emp/${emp.id}">Delete</a></td>
                </tr>
            </c:forEach>
        </table>
    </c:if>
    <br>
    <br>

    <a href="emp">Add New Employee</a>
</body>
</html>


springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <!-- 1.配置所以自动扫描的包 -->
    <context:component-scan base-package="com.yikuan.springmvc"></context:component-scan>
    <!-- 2.配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- <mvc:default-servlet-handler/>将在springMVC上下文中定义一个DefaultServletHttpRequestHandler,他会对进入DispatcherServlet 
        的请求进行筛查,如果发现是没有经过映射的请求,就将该请求交由web应用服务器默认的servlet处理,如果不是静态资源的请求, 才由DispatcherServlet继续处理。一般web应用服务器默认的Servlet的名称都是default,若所使用的web服务器的默认servlet名称不是default, 
        则需要通过default-servlet-name属性显示指定<mvc:default-servlet-handler default-servlet-name="default"/> -->
    <mvc:default-servlet-handler />
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>


java代码

package com.yikuan.springmvc.crud.handlers;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yikuan.springmvc.crud.dao.DepartmentDao;
import com.yikuan.springmvc.crud.dao.EmployeeDao;
import com.yikuan.springmvc.crud.entities.Employee;

@Controller
public class EmployeeHandler {
    
    @Autowired
    private EmployeeDao employeeDao;
    
    @Autowired
    private DepartmentDao departmentDao;
    
    
    @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
    public String delete(@PathVariable("id") Integer id){
        employeeDao.delete(id);
        return "redirect:/emps";
    }
    
    
    @RequestMapping(value="/emp",method=RequestMethod.POST)
    public String save(Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";
    }
    
    
    @RequestMapping(value="emp",method=RequestMethod.GET)
    public String input(Map<String,Object> map){
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", new Employee());
        return "input";
    }
    
    @RequestMapping("/emps")
    public String list(Map<String, Object> map){
        map.put("employees", employeeDao.getAll());
        return "list";
    }
}

 

posted @ 2018-10-03 21:52  yikuandyk  阅读(539)  评论(0编辑  收藏  举报