zoukankan      html  css  js  c++  java
  • Rest风格的CRUD

    index.jsp文件:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    </head>
    <body>
        
        <form action="springmvc/testRest/1" method="post">
            <input type="hidden" name="_method" value="PUT"/>
            <input type="submit" value="TestRest PUT"/>
        </form>
        <br><br>
        
        <form action="springmvc/testRest/1" method="post">
            <input type="hidden" name="_method" value="DELETE"/>
            <input type="submit" value="TestRest DELETE"/>
        </form>
        <br><br>
        
        <form action="springmvc/testRest" method="post">
            <input type="submit" value="TestRest POST"/>
        </form>
        <br><br>
        
        <a href="springmvc/testRest/1">Test Rest Get</a>
        
    </body>
    </html>

    web.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
    
        <!-- 
        配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 PUT 请求
        -->
        <filter>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
        
        <filter-mapping>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!-- 配置 DispatcherServlet -->
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
            <!-- 
                实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
                默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
            -->
            <!--  
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            -->
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    </web-app>

    controller类:

    @RequestMapping("/springmvc")
    @Controller
    public class SpringMVCTest {
    
      /**
        Rest 风格的 URL:
        以 CRUD 为例:
        新增:/order POST
        修改:/order/1 PUT
        获取:/order/1 GET
        删除:/order/1 DELETE

        如何发送 PUT 请求和 DELETE 请求呢?
        1.需要配置 HiddenHttpMethodFilter
        2.需要发送 POST 请求
        3.需要在发送 POST 请求时携带一个 name="_method" 的隐藏域,值为 DELETE 或 PUT
      
        在 SpringMVC 的目标方法中如何得到 id 呢?
        使用 @PathVariabl
      */
        @RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
        public String testRestPut(@PathVariable Integer id) {
            System.out.println("testRest Put: " + id);
            return SUCCESS;
        }
    
        @RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
        public String testRestDelete(@PathVariable Integer id) {
            System.out.println("testRest Delete: " + id);
            return SUCCESS;
        }
    
        @RequestMapping(value = "/testRest", method = RequestMethod.POST)
        public String testRest() {
            System.out.println("testRest POST");
            return SUCCESS;
        }
    
        @RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET)
        public String testRest(@PathVariable Integer id) {
            System.out.println("testRest GET: " + id);
            return SUCCESS;
        }
    
    }
  • 相关阅读:
    iphone精简教程
    自己搭建云盘 – 简单的PHP网盘程序
    内存泄漏(I)
    App 基本图片配置(I)
    Git 工作环境配置
    MVC(I)
    ReactNative APP基本框架搭建 基于 React Navigation
    UI绘制原理及卡顿 掉帧原因
    ES6中Json、String、Map、Object之间的转换
    Invariant Violation: requireNativeComponent: "RNCWKWebView" was not found in the UIManager.
  • 原文地址:https://www.cnblogs.com/maigy/p/12025132.html
Copyright © 2011-2022 走看看