SpringMVC(四):SpringMVC的RequestMapping注解

举报
Lansonli 发表于 2022/12/27 11:36:46 2022/12/27
【摘要】 ​SpringMVC的RequestMapping注解一、@RequestMapping控制请求方式method属性可以控制请求的方式,值为RequestMethod的枚举值@RequestMapping( value = "/***" ,method = RequestMethod.GET)二、@RequestMapping控制请求参数params和请求头headersparam:表示请求...

​SpringMVC的RequestMapping注解

一、@RequestMapping控制请求方式

method属性可以控制请求的方式,值为RequestMethod的枚举值

@RequestMapping( value = "/***" ,method = RequestMethod.GET)


二、@RequestMapping控制请求参数params和请求头headers

param:表示请求中必须包含名为param的参数

!param:表示请求中不能包含名为param的参数

param = value 表示请求中包含名为param的参数,但是值必须是value

param != value 表示请求中包含名为param的参数,但是值不能是value

{"param1","param2=value"},可以将对于多个参数的要求写入数组

@RequestMapping( value = "/***" ,params = {"username!=root","password"})


@RequestMapping( value = "/***",headers = {"Accept-Encoding=gzip, deflate"})


三、@PathVariable注解和RESTful风格的支持

普通形式的url

*****/contextPath/aaa.do

*****/contextPath/aaa.jsp

*****/contextPath/aaa.html

*****/contextPath/css/aaa.css

*****/contextPath/js/aaa.js

*****/contextPath/aaa.do?id=10&username=root

restFul风格的url

*****/contextPath/aaa/10/root

*****/contextPath/aaa

controller 处理单元

@Controller
public class PathController {
    @RequestMapping("/testPathVariable/{id}/{username}")
    public String testPathVariable(@PathVariable("id") Integer id, @PathVariable("username") String username){
        System.out.println("id:"+id);
        System.out.println("username:"+username);
        System.out.println("testPathVariable1");
        return "success";
    }
}


请求测试

Http协议中,四个表示操作方式的动词"GET POST PUT DELETE",他们对应四种基本操作,GET用来获取资源,POST用来新建资源,PUT用来更新资源,DELETE用来删除资源

简单的说,就是我们在访问资源时,可以通过这四个状态来表示对于资源的不同操作,这四个状态表现为我们请求的四种方式

/controller/1 HTTP GET :得到id为1 的资源

/controller/1 HTTP DELETE :删除id为1的资源

/controller/1 HTTP PUT :更新id为1 的资源

/controller/1 HTTP POST :增加id为1 的资源

在访问同一个url的时候,通过不同的请求方式,对应到不同的controller处理单元

1、配置hiddenHttpMethodFilter

 <!--配置hiddenHttpMethodFilter ,将POST请求转换为PUT或者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>


转换原理

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        HttpServletRequest requestToUse = request;
        if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) {
            String paramValue = request.getParameter(this.methodParam);// "_method"
            if (StringUtils.hasLength(paramValue)) {
                String method = paramValue.toUpperCase(Locale.ENGLISH);
                if (ALLOWED_METHODS.contains(method)) {
                    requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method);
                }
            }
        }
        filterChain.doFilter((ServletRequest)requestToUse, response);
    }


2、准备Controller层代码

package com.lanson.controller;
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.RestController;
/**
 * @Author: Lansonli
 * @Description: MircoMessage:Mark_7001
 */
@RequestMapping(value = "/myController")
@RestController
public class MyController {
    @RequestMapping(value = "/testRest/{id}",method = RequestMethod.PUT)
    public String testPut(@PathVariable(value = "id") Integer id){
        System.out.println("testPut, id:"+id);
        return "show";
    }
    @RequestMapping(value = "/testRest/{id}",method = RequestMethod.DELETE)
    public String testDelete(@PathVariable(value = "id") Integer id){
        System.out.println("testDelete, id:"+id);
        return "show";
    }
    @RequestMapping(value = "/testRest/{id}",method = RequestMethod.POST)
    public String testPOST(@PathVariable(value = "id") Integer id){
        System.out.println("testPOST, id:"+id);
        return "show";
    }
    @RequestMapping(value = "/testRest/{id}",method = RequestMethod.GET)
    public String testGET(@PathVariable(value = "id") Integer id){
        System.out.println("testGET, id:"+id);
        return "show";
    }
}


3、准备页面代码

<form action="myController/testRest/10" method="POST">
    <input  type="hidden" name="_method" value="PUT">
    <input type="submit" value="testPUT">
</form>
<br/>
<form action="myController/testRest/10" method="POST">
    <input  type="hidden" name="_method" value="DELETE">
    <input type="submit" value="testDELETE">
</form>
<br/>
<form action="myController/testRest/10" method="POST">
    <input type="submit" value="testPOST">
</form>
<br/>
<form action="myController/testRest/10" method="GET">
    <input type="submit" value="testGET">
</form>
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。