REST风格的URL(主要用在异步请求)

什么是REST风格的URL

之前地址的写法:用不同的地址区分不同的操作,
REST风格的URL:用不同的请求方式来区分不同的操作。
Restful是一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件。主要用于客户端和服 务器交互类的软件,基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存机制等。
Restful风格的请求是使用“url+请求方式”表示一次请求目的的,HTTP 协议里面四个表示操作方式的动词如下:

 GET:读取(Read)
 POST:新建(Create)
 PUT:更新(Update)
 DELETE:删除(Delete

spring webmvc对REST风格的支持

注意:浏览器一般只支持get和post请求方式,对于HTTP协议中的其他请求方式是不支持的。spring webmvc中的@ReuqestMapping不仅能够根据uri地址进行映射,还能通过请求方式进行映射,且地址和方式必须同时满足才能访问到。spring webmvc是通过对post请求方式进行模拟,以达到支持其他请求方式的目的。

代码实现:

  1. 编写请求视图
<html>
<head>
    <title>默认主页</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js"></script>
</head>
<body>
<input id="btnGet" type="button" value="get查询请求">&nbsp;
<input id="btnPost" type="button" value="post添加请求">&nbsp;
<input id="btnPut" type="button" value="put修改请求">&nbsp;
<input id="btnDelete" type="button" value="delete删除请求"><br/>
<script type="text/javascript">
    $(function () {
        $("#btnGet").click(function () {
            $.get("test/test03");
        });
        $("#btnPost").click(function () {
            $.post("test/test03");
        });
        $("#btnPut").click(function () {
            $.post("test/test03","_method=PUT");
        });
        $("#btnDelete").click(function () {
            $.post("test/test03","_method=DELETE");
        });
    })
</script>
</body>
</html>
  1. web.xml中配置一个过滤器(spring是使用一个过滤器完成的模拟)
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                        http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!--配置servlet-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springwebmvc.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--处理中文乱码的过滤器-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--支持REST风格的过滤器-->
    <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>
</web-app>
  1. springwebmvc.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:context="http://www.springframework.org/schema/context"
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--开启扫描-->
    <context:component-scan base-package="cn.itcast.controller"/>
    <!--开启视图-->
    <bean id="viewSolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--开启注解-->
    <mvc:annotation-driven/>
    <!--开放静态资源-->
    <mvc:default-servlet-handler/>
</beans>
  1. 处理方法
package cn.itcast.controller;

import cn.itcast.domain.User;
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 org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("test")
public class TestController {
	@RequestMapping(value = "test03",method = RequestMethod.GET)
    public void test0301(){
        System.out.println("查询用户...");
    }
    @RequestMapping(value = "test03",method = RequestMethod.POST)
    public void test0302(){
        System.out.println("添加用户...");
    }
    @RequestMapping(value = "test03",method = RequestMethod.PUT)
    public void test0303(){
        System.out.println("修改用户...");
    }
    @RequestMapping(value = "test03",method = RequestMethod.DELETE)
    public void test0304(){
        System.out.println("删除用户...");
    }
}

测试,控制台打印出对应的信息就说明OK了。

获取请求URI中的参数@PathVariable

  1. 请求视图
<input type="button" id="btDelete1" value="删除delete请求:带其他请求参数"/>
<script type="text/javascript">
    $(function(){
        $("#btDelete1").click(function(){
            //地址是:test04,后面的1是uid的值
            $.post("test/test041","_method=DELETE");
        });
    })
</script>
  1. 处理方法
//{uid}请求参数占位符  test041
    @RequestMapping(path = "test04{uid}",method = RequestMethod.DELETE)
    //@PathVariable讲地址中的uid占位符取值赋值给处理方法参数uid
    //如果占位符叫做id,处理方法参数名叫做uid,可以@PathVariable("id"),讲占位符id的取值赋值给处理方法的uid参数
    public void test04(@PathVariable("uid") Integer uid){
        System.out.println("删除用户....."+uid);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值