springMVC-3-获取参数
RequestMapping修饰类
源码:
根据源码可以知道,requestmapping既可以修饰方法也可以修饰类
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {}
总结:
在控制器的类定义及方法定义处都可标注 @RequestMapping
- 类定义处:提供初步的请求映射信息。相对于 WEB 应用的根目录
- 方法处:提供进一步的细分映射信息。相对于类定义处的 URL.
- 若 类定义处未标注 @RequestMapping,则方法处标记的 URL 相对于 WEB 应用的根目录
RequestMapping请求属性
标准的 HTTP 请求报头

method属性-请求方式
在注解中添加属性
@RequestMapping(value = "test1",method = RequestMethod.POST)
之前我们在index.jsp中使用的是a来跳转,这是get请求,也需要改成post、
get请求:
<a href="/mycontrol/test1">test1</a>
post请求:
<form action="/mycontrol/test2" method="post">
<input type="submit">
</form>
params、headers属性-请求参数、请求头
params 和 headers支持简单的表达式:
- param1: 表示请求必须包含名为 param1 的请求参数
- !param1: 表示请求不能包含名为 param1 的请求参数
- param1 != value1: 表示请求包含名为 param1 的请求参数,但其值不能为 value1
- {“param1=value1”, “param2”}:
- 请求必须包含名为 param1 和param2 的两个请求参数,
- 且 param1 参数的值必须为 value1
@RequestMapping(value = "test2",
params = {"username","age!=10"},
headers = {"Accept-Language=zh-CN,zh;q=0.9"})

@RequestMapping可以匹配的url
Ant 风格资源地址支持 3 种匹配符:
- ?:匹配文件名中的一个字符
- *:匹配文件名中的任意字符
- **: 匹配多层路径
@RequestMapping 还支持 Ant 风格的 URL:
- /user/*/createUser
- 匹配 /user/aaa/createUser、/user/bbb/createUser 等 URL
- /user/**/createUser
- 匹配 /user/createUser、/user/aaa/bbb/createUser 等 URL
- /user/createUser??
- 匹配 /user/createUseraa、/user/createUserbb 等 URL
@PathVariable映射URL绑定的占位符
可以直接将url里的参数不通过get方式也能传输到servlet程序中
@RequestMapping(value = "test3/{id}")
public String test3(@PathVariable("id") int id){
System.out.println(id+"输出");
System.out.println("占位符");
return "success";
}
REST中的State Transfer状态转换
HTTP 协议里面,四个表示操作方式的动 词:GET、POST、PUT、DELETE。
它们分别对应四种基本操作:
- GET 用来获 取资源
- POST 用来新建资源
- PUT 用来更新资源
- DELETE 用来删除资源。
示例:
/order/1 HTTP GET :得到 id = 1 的 order
/order/1 HTTP DELETE:删除 id = 1的
/order/1 HTTP PUT:更新id = 1的
/order HTTP POST:新增 order
HiddenHttpMethodFilter
- 浏览器form表单只支持get方法和post请求,而delete和put请求方式并不支持
- 而hiddenhttpmethodfilter这个过滤器就可以把post方式转成delete和put请求
怎么转换
需要在web.xml中配置HiddenHttpMethodFilter过滤器
<!--配置转换post为delete和put方式-->
<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>
index.jsp中
- 在DELETE/PUT请求时,需要携带一个name="_method"的隐藏域,值为DELETE/PUT
- name=name="_method" value="PUT"
- 或:name="_method" value="DELETE"
<h1>Rest</h1>
<%--rest--%>
<br>
<form action="/Transfer/test/1" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="testRest PUT"/>
</form>
<br>
<form action="/Transfer/test/1" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" name="_method" value="testRest DELETE">
</form>
<br>
<form action="/Transfer/test" method="post">
<input type="submit" value="testRest POST"/>
</form>
<br>
<a href="/Transfer/test/1">testRest GET</a>
control中
- 在刚刚配置了DELETE/PUT中我们只需要配置方式属性为对应的方式即可
- 如何获取传入的数据:
- 只需要使用@Pathvariable注解即可
@Controller()
@RequestMapping("Transfer")
public class Transfer {
@RequestMapping(value = "test/1",method = RequestMethod.PUT)
public String testRestPut(@PathVariable("id") Integer id){
System.out.println("更新id为1的数据");
return "rest";
}
@RequestMapping(value = "test/1",method = RequestMethod.DELETE)
public String testRestDelete(@PathVariable("id") Integer id){
System.out.println("删除id为1的数据");
return "rest";
}
@RequestMapping(value ="test",method = RequestMethod.POST)
public String testRestPost(){
System.out.println("新加post发送过来的数据");
return "rest";
}
@RequestMapping(value ="test/1",method = RequestMethod.GET)
public String testRestGet(@PathVariable("id") Integer id){
System.out.println("获取id为1的数据");
return "rest";
}
}
出现问题
当我们去访问delete方式和put方式:

后来才知道tomcat8以上是不支持delete方式和put方式
解决方法:
把目标跳转的jsp的头文件改为(加上了isErrorPage="true")
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
这样可以捕获异常继续执行
1、获取基本类型参数
1.1、使用 @RequestParam 获取请求参数值
@PathVariable主要获取的时rest的那种参数
@RequestParam映射请求参数,属性有:
- values:传入参数的键名
- required:这个参数是不是必须存在的
- defaultValue:没有传入时的默认值
@Controller
@RequestMapping("param")
public class Param {
@RequestMapping(value = "test1")
public String test1(@RequestParam(value = "username",required = false,defaultValue = "无名") String username,
@RequestParam(value = "age",required = false,defaultValue = "0") int age){
System.out.println(username);
System.out.println(age);
return "rest";
}
}
1.2、使用 @RequestHeader 获取请求报头的属性值
类似于@RequestParam()一样的获取数据方式
@Controller
@RequestMapping("param")
public class Param {
@RequestMapping(value = "test1")
public String test1(
@RequestParam(value = "username",required = false,defaultValue = "无名") String username,
@RequestParam(value = "age",required = false,defaultValue = "0") int age,
@RequestHeader(value = "Accept-Language") String al){
System.out.println(al);
System.out.println(username);
System.out.println(age);
return "rest";
}
}
1.3、使用 @CookieValue 绑定请求中的 Cookie 值
类似于@RequestParam()一样的获取数据方式
@Controller
@RequestMapping("param")
public class Param {
@RequestMapping(value = "test1")
public String test1(
@RequestParam(value = "username",required = false,defaultValue = "无名") String username,
@RequestParam(value = "age",required = false,defaultValue = "0") int age,
@RequestHeader(value = "Accept-Language") String al,
@CookieValue(value = "JSESSIONID") String cookie){
System.out.println(cookie);
System.out.println(al);
System.out.println(username);
System.out.println(age);
return "rest";
}
}
1.4、使用 Servlet API 作为入参(放入方法中)
MVC 的 Handler 方法可以接受 哪些 ServletAPI 类型的参数
- HttpServletRequest
- HttpServletResponse
- HttpSession
- java.security.Principal
- Locale • InputStream
- OutputStream
- Reader
- Writer
使用实例:
@RequestMapping(value = "test3")
public String test3(HttpServletRequest request, HttpServletResponse response, Writer out) throws IOException {
System.out.println(request);
System.out.println(response);
out.write("write");
System.out.println();
return "rest";
}
2、获取参数-实体收参(POJO)
POJO实体类
@Data
public class User {
private String username;
private String password;
private String email;
//级联属性
private Address address;
}
@Data
public class Address {
private String province;
private String city;
}
index.jsp
<h1>POJO收参</h1>
<form action="/param/test2" method="post">
<br>
username:<input type="text" name="username">
<br>
password:<input type="password" name="password">
<br>
email:<input type="text" name="email">
<%--级联赋值属性--%>
<br>
province:<input type="text" name="address.province">
<br>
city:<input type="text" name="address.city">
<br>
<input type="submit" value="提交">
</form>
control
- 只需要把我们需要实体化的那个类传入方法即可
@RequestMapping(value = "test2")
public String test2(User user){
System.out.println(user);
return "rest";
}
3、数组收参
4、集合收参
5、路径参数
springMVC-3-获取参数的更多相关文章
- springmvc之获取参数
1.导包,四大核心包,一个切面包(AOP),logging,web,springmvc 2.配置文件,核心代码如下: web.xml <servlet> <servlet-name& ...
- springmvc如何获取参数
请参考这篇文章, 写得比较全面. https://www.cnblogs.com/xiaoxi/p/5695783.html
- Servlet Struts2 SpringMVC 获取参数与导出数据 方法比较
servlet中通过request.getParameter()从网页获取参数 通过request session servletContext几个域对象的setAttribute(String ,O ...
- SpringMVC——如何获取请求参数
参考 http://www.cnblogs.com/bigdataZJ/p/springmvc2.html (文章讲了几个注解的使用,但不够深入.) 参考 http://www.cnblogs.com ...
- SpringMVC之请求参数的获取方式
转载出处:https://www.toutiao.com/i6510822190219264516/ SpringMVC之请求参数的获取方式 常见的一个web服务,如何获取请求参数? 一般最常见的请求 ...
- SpringMVC获取参数的几种方式
前言: 年末了,忙了一年了却发现系统的整理的东西很少,一些基础的东西都未做整理,这里就将它随便整理一下,增加一些印象,当然在网上看到一些好的资料也会整理下来以备后用.今天整理一下springMVC获取 ...
- springMVC --@RequestParam注解(后台控制器获取参数)
在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. 1.获 ...
- springmvc对同名参数处理-我们到底能走多远系列(44)
springmvc对同名参数处理 扯淡: 中断发博客几个月,其实蛮不爽的,可能最近太忙太劳累了些,很多总结也没时间写,今天刚好遇到个小问题,就阅读下源码找找乐.因为考虑到网上大多是提供解决问题的方案, ...
- [Spring MVC] - SpringMVC的各种参数绑定方式
SpringMVC的各种参数绑定方式 1. 基本数据类型(以int为例,其他类似):Controller代码: @RequestMapping("saysth.do") publi ...
- springMVC 接收数组参数,mybatis 接收数组参数,mybatis批量插入/批量删除案例
案例是给一个用户赋予多个权限,多个权限用其对应的主键 id 为参数,组成了 一个id数组,传给springMVC,然后springMVC传给mybatis,然后mybatis批量插入.其实类似的场景还 ...
随机推荐
- TorchScript神经网络集成技术
TorchScript神经网络集成技术 create_torchscript_neuropod 将TorchScript模型打包为neuropod包. create_torchscript_neuro ...
- python+selenium基础篇,三种等待方式,显示、隐式、强制等待
1.显示等待: from selenium import webdriver from time import sleep from selenium.webdriver.support.ui imp ...
- 关于switch语句的使用方法---正在苦学java代码的新手之菜鸟日记
输入月份与年份,判断所输入的月份有多少天. switch支持和不支持的类型 支持的类型 int 类型 short 类型 byte 类型 char 类型 enum (枚举)类型 (java5.0 之后支 ...
- python取整函数 向上取整 向下取整 四舍五入
向上取整 >>> import math >>> math.ceil(3.5) 4 >>> math.ceil(3.4) 4 >>&g ...
- 【NX二次开发】Block UI 选项卡控件
[NX二次开发]Block UI 选项卡控件
- 【Linux进阶】使用grep、find、sed以及awk进行文本操作
目录 一.元字符 二.grep命令 1. 过滤出包含某字符串的行 2. 过滤出以某字符串开头(结尾)的行 3. 过滤出包含某字符串及其相邻的行 4. 过滤出不包含某关键字的行 5. 过滤出包含多个字符 ...
- 20201123 实验一《Python程序设计》实验报告
20201123 2020-2021-2 <Python程序设计>实验一报告 课程:<Python程序设计> 班级:2011班 姓名:晏鹏捷 学号:20201123 实验教师: ...
- Unity 按空格一直触发Button点击事件的问题
#解决 这是由于Button中Navigation(导航)功能导致的. 将导航设置为None即可. 真是气死我了,我说为什么点击完按钮界面,按空格就一直触发界面,难搞
- 一、RabbitMQ 概念详解和应用
消息队列和同步请求的区别 无论RabbitMQ还是Kafka,本质上都是提供了基于message或事件驱动异步处理业务的能力,相比于http和rpc的直接调用,它有着不可替代的优势: 1. 解耦,解耦 ...
- Redis i/o timeout
1.背景 公司项目使用国外ucloud云,发现公司业务服务器时常连接redis服务,发生i/o timeout的问题.研发以及服务器侧查看没有异常,反馈给ucolud解决问题.所以这里做一个记录. 2 ...