• 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";
        }
    }
  • 相关阅读:
    Android Listview 隐藏滚动条
    打开Activity时,不自动显示(弹出)虚拟键盘
    Spring Boot web API接口设计之token、timestamp、sign
    WPF ListView点击删除某一行并获取绑定数据
    WPF中控件的显示与隐藏
    WPF 格式化输出- IValueConverter接口的使用 datagrid列中的值转换显示
    WPF之DataGrid应用 翻页
    WPF中修改DataGrid单元格值并保存
    DataGrid获取单元格的值
    WPF DataGrid 列宽填充表格方法
  • 原文地址:https://www.cnblogs.com/yikuan-919/p/9741004.html
Copyright © 2020-2023  润新知