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 ...
随机推荐
- 读取pdf内容分页和全部
//读取pdf 全部内容public static String topdffile(String pdffile){ StringBuffer result = new StringBuffer() ...
- HashMap源码解析(JDK1.8)
package java.util; import sun.misc.SharedSecrets; import java.io.IOException; import java.io.Invalid ...
- C语言--第0次作业
1.你认为大学的学习生活.同学关系.师生应该是怎样?请一个个展开描写. 学习生活 大学是一个过渡时间,它不同于高中与社会.我希望自己可以养成自主学习的习惯,也希望能在大学学习中找到属于自己的节奏,不被 ...
- eclipse的Debug模式下的快捷键
主要快捷键: F5, F6, F7, F8的使用 F5: 进入当前方法 F6: 一步步执行 F7: 跳出方法, 返回到调用此方法的最后一条语句 F8: 继续执行,跳转到下一个断点的位置 示例: 在 ...
- 跨域的另一种解决方案CORS(CrossOrigin Resource Sharing)跨域资源共享
在我们日常的项目开发时使用AJAX,传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片 ...
- CAS 之 Hello World(二)
CAS 之 Hello World(二) 标签(空格分隔): CAS Intro(介绍) 由上节可知Apereo CAS官方推荐使用 WAR Overlay 的方式进行部署: It is recomm ...
- Matlab绘图基础——给图像配文字说明(text对象)
text对象 (1)text(x坐标,y坐标,'string')在图形中指定位置(x,y)显示字符串string.(2)Editing有效值为on/off,off时,用户在执行GUI操作时无法直接 ...
- 修改GeoJson的网址
http://geojson.io 可以打开自己的json 然后修改
- selenium2自动化测试学习笔记(四)
今天是学习selenium2第四天.总结下今天的学习成果,自动登录网易邮箱并写信发送邮件. 知识点or坑点: 1.模块化编写测试模块(类似java里的抽象方法,js的函数编写) from 包名 imp ...
- 高校征信系统项目Postmortem结果
设想和目标 1 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件需要解决的问题是当前高校学生征信系统建设薄弱的问题,我们试图建立一个征信系统,在完成之后推 ...