SpringMVC:SpringMVC的RequestMapping注解
Posted Lansonli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC:SpringMVC的RequestMapping注解相关的知识,希望对你有一定的参考价值。
文章目录
二、@RequestMapping控制请求参数params和请求头headers
三、@PathVariable注解和RESTful风格的支持
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>
- 📢博客主页:https://lansonli.blog.csdn.net
- 📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
- 📢本文由 Lansonli 原创,首发于 CSDN博客🙉
- 📢停下休息的时候不要忘了别人还在奔跑,希望大家抓紧时间学习,全力奔赴更美好的生活✨
以上是关于SpringMVC:SpringMVC的RequestMapping注解的主要内容,如果未能解决你的问题,请参考以下文章
SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系
SpringMVC之使用requestMapping映射请求映射参数映射头
如何使用从 Spring MVC 发回的 JSON 对象填充 jQuery 数据表的行?