1. Web接口就是一个url
    2. Restful风格的API是一种软件架构风格,是设计风格而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
    3. Restful风格中,用户请求的url使用同一个url,而用请求方式:getpostdeleteput...等对请求的处理方法进行区分,这样可以在前后台分离式的开发中使得前端开发人员不会对请求的资源地址产生混淆和大量的检查方法名的麻烦,形成一个统一的接口。
    4. 传统方式:/adduser /user/add.do
    5. /queryUser?id=1
    6. /delUser?id=1
    7. /upadeUser?id=1
    8. Restful /user 提交方式post 添加
    9. /user/1 提交方式 get 查询
    10. /user/1 提交方式 delete 删除
    11. /user/1 提交方式put 修改
    12. /user 提交方式get
    13. /user 提交方式put
    14. /dept/1/emp/1 提交方式delete
    15. Restful风格中,现有规定如下:
    16. GETSELECT):从服务器查询,可以在服务器通过请求的参数区分查询的方式。
    17. POSTCREATE):在服务器新建一个资源,调用insert操作。
    18. PUTUPDATE):在服务器更新资源,调用update操作。
    19. DELETEDELETE):从服务器删除资源,调用delete语句。
    20. 要求:除了登录是post 其他都是get,针对于查询来说
    21. 例如:
    22. /users 查询所有用户GET
    23. /users/2 查询指定id的用户 GET
    24. /users/3 删除指定id的用户 DELETE
    25. /users 添加用户 POST
    26. /users/3 更新指定id的用户 PUT

    pom文件

    1. <packaging>war</packaging>
    2. <dependencies>
    3. <!-- servlet jar -->
    4. <dependency>
    5. <groupId>javax.servlet</groupId>
    6. <artifactId>javax.servlet-api</artifactId>
    7. <version>3.1.0</version>
    8. <scope>provided</scope>
    9. </dependency>
    10. <dependency>
    11. <groupId>org.springframework</groupId>
    12. <artifactId>spring-context</artifactId>
    13. <version>5.1.5.RELEASE</version>
    14. </dependency>
    15. <!-- springmvc -->
    16. <dependency>
    17. <groupId>org.springframework</groupId>
    18. <artifactId>spring-web</artifactId>
    19. <version>5.1.5.RELEASE</version>
    20. </dependency>
    21. <dependency>
    22. <groupId>org.springframework</groupId>
    23. <artifactId>spring-webmvc</artifactId>
    24. <version>5.1.5.RELEASE</version>
    25. </dependency>
    26. <dependency>
    27. <groupId>org.slf4j</groupId>
    28. <artifactId>slf4j-log4j12</artifactId>
    29. <version>1.7.2</version>
    30. </dependency>
    31. <dependency>
    32. <groupId>org.hibernate</groupId>
    33. <artifactId>hibernate-validator</artifactId>
    34. <version>5.0.1.Final</version>
    35. </dependency>
    36. <!-- 文件上传 -->
    37. <dependency>
    38. <groupId>commons-fileupload</groupId>
    39. <artifactId>commons-fileupload</artifactId>
    40. <version>1.3.3</version>
    41. </dependency>
    42. </dependencies>
    43. <build>
    44. <resources>
    45. <!--需要设置打包的文件 特别是非java文件-->
    46. <resource>
    47. <directory>src/main/java</directory>
    48. <includes>
    49. <include>**/*.xml</include>
    50. </includes>
    51. <filtering>false</filtering>
    52. </resource>
    53. <resource>
    54. <directory>src/main/resources</directory>
    55. <includes>
    56. <include>**/*.xml</include>
    57. <include>**/*.properties</include>
    58. </includes>
    59. </resource>
    60. </resources>
    61. <plugins>
    62. <!-- 编译插件 -->
    63. <plugin>
    64. <groupId>org.apache.maven.plugins</groupId>
    65. <artifactId>maven-compiler-plugin</artifactId>
    66. <version>3.1</version>
    67. <configuration>
    68. <source>1.8</source>
    69. <target>1.8</target>
    70. <compilerVersion>1.8</compilerVersion>
    71. <encoding>UTF-8</encoding>
    72. </configuration>
    73. </plugin>
    74. <!-- tomcat插件 -->
    75. <plugin>
    76. <groupId>org.apache.tomcat.maven</groupId>
    77. <artifactId>tomcat7-maven-plugin</artifactId>
    78. <version>2.2</version>
    79. <configuration>
    80. <uriEncoding>UTF-8</uriEncoding>
    81. <path>/springmvc</path>
    82. </configuration>
    83. </plugin>
    84. </plugins>
    85. </build>
    86. </project>

    web.xml配置文件

     <!-- post 方式的中文乱码解决 -->
        <filter>
            <filter-name>characterEncoding</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>utf8</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>characterEncoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!--引入spring mvc的核心控制器 配置 -->
        <servlet>
            <servlet-name>DispatchServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 指定加载哪个配置文件 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc1.xml</param-value>
            </init-param>
            <!-- tomcat服务器启动时,创建servlet对象 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>DispatchServlet</servlet-name>
            <url-pattern>/</url-pattern>
            <!--<url-pattern>*.do</url-pattern>-->
        </servlet-mapping>
    

    web.xml需要增加如下配置:
    <!—配置HiddenHttpMethodFilter,将 post转为 put or delete提交方式 —

     <!-- 配置HiddenHttpMethodFilter,将 post转为 put or delete提交方式 -->
        <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>
    

    springmvc各项配置文件

    <?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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!--    扫描控制器的注解-->
        <context:component-scan base-package="com.qfedu"></context:component-scan>
        <!--        在日期格式化的时候遇到的-->
        <mvc:annotation-driven ></mvc:annotation-driven>
        <!--    第一种方式:将静态资源的处理经由Spring MVC框架交回Web应用服务器处理-->
        <mvc:default-servlet-handler></mvc:default-servlet-handler>
        <!--    处理静态资源的方案2,但是我不用。-->
        <!--    静态资源的映射
            location项目中的真实路径
            mapping将真实的路径映射给当前起名字的路径
        -->
        <!--    <mvc:resources mapping="/js123/**" location="/js/"></mvc:resources>-->
    
    </beans>
    

    控制层

    package com.qfedu.rest;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    
    @Controller
    public class RestController {
        //现在是get请求  查询一条数据
        // @RequestMapping(value = "/user", method = RequestMethod.GET)
        @GetMapping("/user")
        public String findInfo(){
            System.out.println("get info");
            return "/index.jsp";
        }
        //增加一条数据
        //@RequestMapping(value = "/user", method = RequestMethod.POST)
        @PostMapping("/user")
        public String addUser(){
            System.out.println("post info");
            return "/index.jsp";
        }
        //删除某一条数据
        //@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
        @DeleteMapping("/user/{id}")
        //PathVariable 获取路径中的变量
        public String deleteInfo (@PathVariable("id") Integer id) {
            System.out.println(id);
            return "/index.jsp";
        }
        //修改某一条数据
        //@RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
        @PutMapping("/user/{id}")
        //PathVariable 获取路径中的变量
        public String updateinfo (@PathVariable("id") Integer id) {
            System.out.println(id);
            return "/index.jsp";
        }
    
    }
    

    前端jsp文件

    <%--
      Created by IntelliJ IDEA.
      User: wangbo
      Date: 2021/12/8
      Time: 9:30
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <form action="user" method="get">
            <input type="submit" value="get">
        </form>
        <form action="user" method="post">
            <input type="submit" value="post">
        </form>
        <form action="user/2" method="post">
            <input type="hidden" name="_method" value="delete">
            <input type="submit" value="delete">
        </form>
        <form action="user/4" method="post">
            <input type="hidden" name="_method" value="put">
            <input type="submit" value="update">
        </form>
    </body>
    </html>