SpringMVC(五):@RequestMapping下使用@RequestParam绑定请求参数值
在处理方法入参使用@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
SpringMVC(五):@RequestMapping下使用@RequestParam绑定请求参数值的更多相关文章
- 【SpringMVC】SpringMVC系列7之POJO 对象绑定请求参数值
7.POJO 对象绑定请求参数值 7.1.概述 Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值.而且支持级联属性.如:dept.deptId.dept ...
- SpringMVC(六):@RequestMapping下使用@RequestHeader绑定请求报头的属性值、@CookieValue绑定请求中的Cookie值
备注:我本地浏览器的报头(Request Header)信息如下: Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image ...
- 【SpringMVC】SpringMVC系列4之@RequestParam 映射请求参数值
4.@RequestParam 映射请求参数值 4.1.概述 Spring MVC 通过分析处理方法的签名,将 HTTP 请求信息绑定到处理方法的相应人参中.Spring MVC 对控制器处理 ...
- 使用@RequestParam绑定请求参数到方法参数
@RequestParam注解用于在控制器中绑定请求参数到方法参数.用法如下:@RequestMapping public void advancedSearch( @RequestParam(& ...
- SpringMVC(七):@RequestMapping下使用POJO对象绑定请求参数值
Spring MVC会按照请求参数名和POJO属性名进行自动匹配,自动为该对象填充属性值,支持级联属性. 如:address.city.dept.address.province等. 步骤一:定义Ac ...
- SpringMVC(五) RequestMapping 请求参数和请求头
可以通过在@RequestMapping的params参数中设置可以传入的参数,且支持简单的表达式,如以下格式: @RequestMapping(value="helloRWorld&quo ...
- SpringMVC系列(四)使用 POJO 对象绑定请求参数值
在实际开发中如果参数太多就不能使用@RequestParam去一个一个的映射了,需要定义一个实体参数对象(POJO)来映射请求参数.Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配 ...
- SpringMVC学习 -- 使用 POJO 对象绑定请求参数值
Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配 , 自动为该对象填充属性值 , 支持级联属性.如:address.province. package com.itdoc.spri ...
- SpringMVC之使用 POJO 对象绑定请求参数值
Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值.支持级联属性.如:dept.deptId.dept.address.tel 等 示例: User实体类 p ...
随机推荐
- elasticsearch基本操作之--java基本操作 api
/** * 系统环境: vm12 下的centos 7.2 * 当前安装版本: elasticsearch-2.4.0.tar.gz */ 默认进行了elasticsearch安装和ik安装, 超时配 ...
- canvas实现将文字变成颗粒
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 关于标准ui设计图转换为H5页面的终端适配
一些基本概念 在进行具体实战之前,首先得了解下面这些基本概念(术语): 视窗 viewport 简单的理解,viewport是严格等于浏览器的窗口.在桌面浏览器中,viewport就是浏览器窗口的宽度 ...
- C语言第七次博客作业--一二维数组
一.PTA实验作业 题目1:找鞍点 1. 本题PTA提交列表 2. 设计思路 定义n,i,j,ii,jj,a[7][7],flag,max 输入n for i=0 to i=n for j=0 to ...
- angularjs常用事件
1. 视图模板加载完毕 $scope.$on('$viewContentLoaded', function() { alert('view template loaded'); });
- Python实现制度转换(货币,温度,长度)
人民币和美元是世界上通用的两种货币之一,写一个程序进行货币间币值转换,其中: 人民币和美元间汇率固定为:1美元 = 6.78人民币. 程序可以接受人民币或美元输入,转换为美元或人民币输出.人民币采用R ...
- 【itchat】用Python玩耍微信
[itchat] itchat是个基于网页版微信的python微信API.功能目前做到基本可以满足正常的消息收发,信息的获取等等.不过对于红包之类网页版微信不支持的功能,这个模块自然也就无法支持了. ...
- 【Python】 list & dict & str
list & dict & str 这三种类型是python中最常用的几种数据类型.他们都是序列的一种 ■ 序列通用操作 1. 分片 s[a:b] 返回序列s中从s[a]到s[b- ...
- Spring Boot入门教程1、使用Spring Boot构建第一个Web应用程序
一.前言 什么是Spring Boot?Spring Boot就是一个让你使用Spring构建应用时减少配置的一个框架.约定优于配置,一定程度上提高了开发效率.https://zhuanlan.zhi ...
- c++ --> 父类与子类间的继承关系
父类与子类间的继承关系 一.父类与子类 父类与子类的相互转换 1.派生类的对象可以赋给基类,反之不行 2.基类的指针可以指向派生类,反之不行 3.基类的引用可以初始化为派生类的对象,反之不行 4.派生 ...