SpringMVC——RequestMapping
一、@RequestMapping 映射请求
Spring MVC 通过@RequestMapping注解可以定义不同的处理器映射规则。
- @RequestMapping放在类名上边,设置请求前缀
方法名上边设置请求映射url
二、映射请求参数、请求方法或请求头
@RequestMapping 除了可以使用请求 URL 映射请求外, 还可以使用请求方法、请求参数及请求头映射请求
1、@RequestMapping 的 value、method、params 及 heads 分别表示请求 URL、请求方法、请求参数及请求头的映射条件,他们之间是与的关系,联合使用多个条件可让请求映射 更加精确化。
/**
* 常用: 使用 method 属性来指定请求方式
*/
@RequestMapping(value = "/testMethod", method = RequestMethod.POST)
public String testMethod() {
System.out.println("testMethod");
return SUCCESS;
}
2、params 和 headers支持简单的表达式:
- param1: 表示请求必须包含名为 param1 的请求参数
- !param1: 表示请求不能包含名为 param1 的请求参数
- param1 != value1: 表示请求包含名为 param1 的请求参数,但其值 不能为 value1
- {“param1=value1”, “param2”}: 请求必须包含名为 param1 和param2 的两个请求参数,且 param1 参数的值必须为 value1
/**
* 可以使用 params 和 headers 来更加精确的映射请求.
* 请求中必须包含username请求参数,age请求参数的值不能等于10
*/
@RequestMapping(value = "testParamsAndHeaders", params = { "username",
"age!=10" }, headers = { "Accept-Language=en-US,zh;q=0.8" })
public String testParamsAndHeaders() {
System.out.println("testParamsAndHeaders");
return SUCCESS;
}
三、@PathVariable 映射 URL 绑定的占位符
- 带占位符的 URL 是 Spring3.0 新增的功能,该功能在 SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义
- 通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。
/**
* @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中.
* @param id
* @return
*/
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id) {
System.out.println(id);
return SUCCESS;
}
四、HiddenHttpMethodFilter:
浏览器 form 表单只支持 GET 与 POST 请求,而DELETE、PUT 等 method 并不支 持,Spring3.0 添加了一个过滤器,可以将这些请求转换 为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。
如何发送 PUT 请求和 DELETE 请求呢 ?
1. 需要配置 HiddenHttpMethodFilter
<!--
配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 POST 请求
-->
<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>
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package org.springframework.web.filter; import java.io.IOException;
import java.util.Locale;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter; public class HiddenHttpMethodFilter extends OncePerRequestFilter {
public static final String DEFAULT_METHOD_PARAM = "_method";
private String methodParam = "_method"; public HiddenHttpMethodFilter() {
} public void setMethodParam(String methodParam) {
Assert.hasText(methodParam, "\'methodParam\' must not be empty");
this.methodParam = methodParam;
} protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String paramValue = request.getParameter(this.methodParam);
if("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
String method = paramValue.toUpperCase(Locale.ENGLISH);
HiddenHttpMethodFilter.HttpMethodRequestWrapper wrapper = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method);
filterChain.doFilter(wrapper, response);
} else {
filterChain.doFilter(request, response);
} } private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
private final String method; public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
super(request);
this.method = method;
} public String getMethod() {
return this.method;
}
}
}
2.需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="TestRest PUT"/>
</form> <form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="TestRest DELETE"/>
</form> <form action="springmvc/testRest" method="post">
<input type="submit" value="TestRest POST"/>
</form> <a href="springmvc/testRest/1">Test Rest Get</a>
3.@RequestMapping添加method属性
@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;
}
SpringMVC——RequestMapping的更多相关文章
- SpringMVC RequestMapping 详解
SpringMVC RequestMapping 详解 RequestMapping这个注解在SpringMVC扮演着非常重要的角色,可以说是随处可见.它的知识点很简单.今天我们就一起学习Spring ...
- SpringMVC RequestMapping注解
1.@RequestMapping 除了修饰方法,还可以修饰类 2.类定义处:提供初步的请求映射信息.相对于WEB应用的根目录 方法处:提供进一步细分映射信息 相对于类定义处的URL.若类定义处未 ...
- SpringMVC RequestMapping & 请求参数
SpringMVC 概述 Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一 Spring3.0 后全面超越 Struts2,成为最优秀的 MVC ...
- SpringMvc @RequestMapping原理
讲这个之前,我们得先知道在SpringMvc启动时,会加载所有的Bean类,就是加了@Controller,@Component等组件标识的类,然后会把@RequestMapping的方法也加入到一个 ...
- SpringMVC -- @RequestMapping -- 随记
@RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestMappi ...
- JAVA框架 SpringMVC RequestMapping讲解
一.窄化请求映射 在class上做RequestMapping注解. 好处:避免在同一个项目中和其他人的url重复,出现请求混乱问题,便于管理. @Controller @RequestMapping ...
- SpringMVC @RequestMapping 用法详解之地址映射
@RequestMapping 用法详解之地址映射 http://blog.csdn.net/walkerjong/article/details/7994326
- SpringMVC @RequestMapping注解详解
@RequestMapping 参数说明 value:定义处理方法的请求的 URL 地址.(重点) method:定义处理方法的 http method 类型,如 GET.POST 等.(重点) pa ...
- 超详细 SpringMVC @RequestMapping 注解使用技巧
@RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...
随机推荐
- [BZOJ5297][CQOI2018]社交网络
bzoj luogu sol 就是求以\(1\)为根的生成树的数量. 直接矩阵树定理. code #include<cstdio> #include<algorithm> #i ...
- OPCDAAuto.dll的C#使用方法浅析
上次研究了.Net版本的OPC API dll,这次我采用OPCDAAuto.dll来介绍使用方法.以下为我的源代码,有详细的注释无需我多言.编译平台:VS2008SP1.WINXP.KEPServe ...
- juc线程池原理(一):总体介绍
概要 线程池类图 线程池的类图如下: 1. Executor 它是"执行者"接口,它是来执行任务的.准确的说,Executor提供了execute()接口来执行已提交的 Runna ...
- Kibana5.6安装
Kibana5.6安装 1.下载 wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.8-linux-x86_64.tar.gz ...
- python开发函数进阶:匿名函数
一,匿名函数 #简单的需要用函数去解决的问题 匿名函数的函数体 只有一行#也叫lambda表达式# cal2(函数名) = lambda n(参数) : n*n(参数怎么处理,并且返回值)#参数可以有 ...
- Py修行路 python基础 (二十三)模块与包
一.模块 1)定义: 模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 2)为何要用模块: 退出python解释器然后重新进入,那之前定义的函数或者变量都将丢失,因 ...
- get与post两种方式的优缺点
get: get是从服务器上获取数据,post是向服务器传送数据: get传送的数据量较小,不能大于2KB.post传送的数据量较大,一般被默认为不受限制.但理论上,IIS4中最大量为80KB,IIS ...
- Python实现SSH传输文件(sftp)
Windows通过ssh给Linux发送文件 #-*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import paramiko t ...
- curl 命令参数
curl -X POST -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Cache-Control: ...
- 自己写着玩的一个天气APP
打开的界面: 向上滑动,进入主界面: 省份界面: 城市界面: 加载天气界面: 显示天气界面: 侧滑,显示地区,然后根据天气来显示一首诗句(晴,多云,雪,雨什么的): 第一次启动App的时候才会加载数据 ...