SpringMVC(五):@RequestMapping下使用@RequestParam绑定请求参数值

Easter79
• 阅读 648

在处理方法入参使用@RequestParam可以把请求参数传递给请求方法,@RequestParam包含的属性值:

--- value :参数名称

--- required :是否必须,默认为true,表示请求参数中必须包含对应的参数,否则抛出异常。

--- defaultValue:当请求参数缺少或者有请求参数但值为空时,值采用该设置值。

示例:

1)在HelloWord.java中添加testRequestParam方法:

package com.dx.springlearn.handlers;

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.RequestParam;

@Controller
@RequestMapping("class_requestmapping")
public class HelloWord {
    private static String SUCCESS = "success";

    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(value = "username") String username,
            @RequestParam(value = "address") String address,
            @RequestParam(value = "age", required = false, defaultValue = "0") int age) {
        System.out.println("testRequestParam, username: " + username + ",address: " + address + ",age: " + age);
        return SUCCESS;
    }
}

2)在index.jsp中插入链接html:

<a
        href="class_requestmapping/testRequestParam?username=abc&address=def&age=26">testRequestParam</a>
    <br>

3)测试。

当请求url为:http://localhost:8080/SpringMVC\_01/class\_requestmapping/testRequestParam?username=abc&address=def&age=26

打印信息为:testRequestParam, username: abc,address: def,age: 26

当请求url为:http://localhost:8080/SpringMVC\_01/class\_requestmapping/testRequestParam?username=abc&address=def&age=

抛出异常:

Jan 04, 2018 8:02:53 PM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleTypeMismatch
警告: Failed to bind request element: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: ""

解决方案,把age定义类型修改为Integer

当请求url为:http://localhost:8080/SpringMVC\_01/class\_requestmapping/testRequestParam?username=abc&address=def

打印信息为:testRequestParam, username: abc,address: def,age: 0

当请求url为:http://localhost:8080/SpringMVC\_01/class\_requestmapping/testRequestParam?username=abc

抛出异常:

HTTP Status 400 - Required String parameter 'address' is not present
点赞
收藏
评论区
推荐文章
开放API接口签名验证,让你的接口从此不再裸奔
接口安全问题请求身份是否合法?请求参数是否被篡改?请求是否唯一?AccessKey&SecretKey(开放平台)请求身份为开发者分配AccessKey(开发者标识,确保唯一)和SecretKey(用于接口加密,确保不易被穷举,生成算法不易被猜测)。防止篡改参数签名1.按照请求参数名的字母升序排列非空请求参数(包含AccessK
LinMeng LinMeng
3年前
vue中页面间跳转传值的两种方式(query,params)
两者都可以传递参数,区别是什么?query传参配置的是path,而params传参配置的是name,在params中配置path无效query在路由配置不需要设置参数,而params必须设置query传递的参数会显示在地址栏中params传参刷新会无效,但是query会保存传递过来的值,刷新不变;query:this.$route
Easter79 Easter79
2年前
springMVC笔记系列(8)——RequestParam注解
前面的文章介绍过注解@PathVariable,它能够为Rest风格的URL用占位符的方式传递一个参数,但是这个参数并不是真正意义上的请求参数。请求参数怎么处理是本文的主要内容。SpringMVC通过分析处理方法的签名,将HTTP请求信息绑定到处理方法的相应人参中。SpringMVC对控制器处理方法签名的限制是很宽松的,几乎可以按喜欢的任
Stella981 Stella981
2年前
Spring Boot学习(四)Controller接收请求参数
SpringBoot学习(四)Controller接收请求参数一、通过实体Bean接收请求参数通过实体Bean来接收请求参数,适用于get和post方式,Bean的属性名称必须与请求参数名称相同。项目结构如下:!(https://static.oschina.net/uploads/space/2018/0601/174959_FvRV
Easter79 Easter79
2年前
SpringFramework之ContentNegotiation内容协商
  Spring版本5.1.4.release.  内容协商是用在Springmvc返回Controller方法结果序列化时使用,而不是解析mvc参数时使用。    Springmvc支持4种内容协商,拓展名、固定值、Http的头部Accept、请求参数format,那Springmvc中怎么实现的呢,怎么使用已经有很多人分析了,这里来分析下
Stella981 Stella981
2年前
Spring Boot 406(type=Not Acceptable, status=406)异常解决办法
使用SpringBoot,Controller请求返回的参数类型是ResponseBody,如果请求的时候使用使用配置的默认请求扩展名,例如.html,SpringMVC会抛出一个typeNotAcceptable,status406错误,如下:WhitelabelErrorPageThisapplica
Easter79 Easter79
2年前
SpringMVC 页面传递参数到controller的五种方式
一共是五种传参方式:一:直接将请求参数名作为Controller中方法的形参public Stringlogin(Stringusername,Stringpassword) :解释:括号中的参数必须与页面Form表单中的name名字相同二:使用@RequestParam绑定请求参数参数值举例:publicStri
Stella981 Stella981
2年前
SpringBoot2 学习10 Controller接收参数的方式
地址传值@PathVariable获取路径参数。即url/{id}这种形式。?传值@RequestParam获取查询参数。即url?name这种形式用注解@RequestParam绑定请求参数到方法入参当请求参数username不存在时会有异常发生,可以通过设置属性requiredfalse解决,例如:@R
Wesley13 Wesley13
2年前
Java乱码
1.Javascript传参乱码:在浏览器端对要传递的中文参数进行编码处理.代码如下:xmlhttp.open("POST",url,true);//请求参数初始化xmlhttp.setRequestHeader("ContentType","application/xwwwformurlencoded");//因为请求方式为PO
Easter79 Easter79
2年前
SpringBoot2 学习10 Controller接收参数的方式
地址传值@PathVariable获取路径参数。即url/{id}这种形式。?传值@RequestParam获取查询参数。即url?name这种形式用注解@RequestParam绑定请求参数到方法入参当请求参数username不存在时会有异常发生,可以通过设置属性requiredfalse解决,例如:@R
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k