SpringMVC(七):@RequestMapping下使用POJO对象绑定请求参数值
Spring MVC会按照请求参数名和POJO属性名进行自动匹配,自动为该对象填充属性值,支持级联属性。
如:address.city.dept.address.province等。
步骤一:定义Account.java,Address.java类:
package com.dx.springlearn.entities; public class Account {
private String username;
private String password;
private Integer age;
private Address address; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Address getAddress() {
return address;
} public void setAddress(Address address) {
this.address = address;
} @Override
public String toString() {
return "Account [username=" + username + ", password=" + password + ", age=" + age + ", address=" + address
+ "]";
} }
package com.dx.springlearn.entities; public class Address {
private String province;
private String city;
private String details; public String getProvince() {
return province;
} public void setProvince(String province) {
this.province = province;
} public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public String getDetails() {
return details;
} public void setDetails(String details) {
this.details = details;
} @Override
public String toString() {
return "Address [province=" + province + ", city=" + city + ", details=" + details + "]";
} }
步骤二:在HelloWord.java控制类内添加testPojo方法:
package com.dx.springlearn.handlers; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import com.dx.springlearn.entities.Account; @Controller
@RequestMapping("class_requestmapping")
public class HelloWord {
private static String SUCCESS = "success"; @RequestMapping("/testPojo")
public String testPojo(Account account) {
System.out.println("testPojo: account:" + account);
return SUCCESS;
}
}
步骤三:在index.jsp中添加表单提交html脚本:
<form name="testPojo" method="POST"
action="class_requestmapping/testPojo">
username:<input type="text" name="username" /> <br>
password:<input type="password" name="password" /> <br>
age:<input type="text" name="age" /> <br>
address:<br>
province:<input type="text" name="address.province" /> <br>
city:<input type="text" name="address.city" /> <br>
details:<input type="text" name="address.details" /> <br>
<input type="submit" value="Submit">
</form>
<br>
步骤四:测试
提交表单后,打印结果:
testPojo: account:Account [username=abc123, password=123456, age=28, address=Address [province=zhejiang, city=hangzhou, details=hangzhou huo che zhan]]
SpringMVC(七):@RequestMapping下使用POJO对象绑定请求参数值的更多相关文章
- 【SpringMVC】SpringMVC系列7之POJO 对象绑定请求参数值
7.POJO 对象绑定请求参数值 7.1.概述 Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值.而且支持级联属性.如:dept.deptId.dept ...
- 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 ...
- 使用 POJO 对象绑定请求参数
概述 Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值并且支持级联属性.这一特性在日常开发过程中使用频率比较高,开发效率也高,本文主要对 POJO 对象绑定 ...
- 008 使用POJO对象绑定请求参数
1.介绍 2.Person.java package com.spring.bean; public class Person { private String username; private S ...
- SpringMVC(五):@RequestMapping下使用@RequestParam绑定请求参数值
在处理方法入参使用@RequestParam可以把请求参数传递给请求方法,@RequestParam包含的属性值: --- value :参数名称 --- required :是否必须,默认为true ...
- SpringMVC(七) RequestMapping 路径中带占位符的URL
使用方法:在@RequestMapping("/delete/{id}")中,通过{id}带入pathvariable,然后在方法中,通过@PathVariable("变 ...
- 数据绑定-POJO对象绑定参数
测试: 效果:
随机推荐
- 快速排序及优化(Java实现)
普通快速排序 找一个基准值base,然后一趟排序后让base左边的数都小于base,base右边的数都大于等于base.再分为两个子数组的排序.如此递归下去. public class QuickSo ...
- [模拟赛] T2 不等数列
Description 将1到n任意排列,然后在排列的每两个数之间根据他们的大小关系插入">"和"<".问在所有排列中,有多少个排列恰好有k个&qu ...
- java编程基础知识及常见例题
⒈标识符: 只能包含数字.字母.下划线.$,并且不能以数字开头.语义直观规范 驼峰法则: 如:方法名.变量名采用驼峰法则 帕斯卡命名法: 如: 类.接口.枚举采用帕斯卡命名法包名:网址倒写,com.网 ...
- React 系列文章(1): npm 手动搭建React 运行实例 (新手必看)
摘 要 刚接触React 开发, 在摸索中构建react 运行环境,总会遇到各种坑:本文,将用最短时间解决webpack+react 环境搭建问题. 1.如果你还没有React基础 看这里. 2.如果 ...
- poj-1005-l tanink i need a houseboat
Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In t ...
- 【Python】 更棒的Excel操作模块xlwings
[xlwings] 说到Python操作Excel,有好多模块都可以支持这个工作.比如最底层的win32模块不仅可以操作Excel,还可以操作其他一众windows的软件. 其他的比较熟悉的有xlrd ...
- Docker自动化部署方案
一 概述 Docker发布版本应该与现有的版本发布尽量一致,参考jenkins的版本发布过程:我认为maven库和docker库有很多类似的地方,因此打包过程参考maven的打包过程:重点实现dock ...
- linux --> gcc编译之路径搜索
gcc编译之路径搜索 头文件 --> 搜寻先从-I开始; --> 找gcc的环境变量 : C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC_INCLUDE_PA ...
- java数组排序,并将数组内的数据求和
java数据编列并求和,江湖我狼哥,人狠话不多,直接上代码! import java.util.Arrays; public class Intarry { public static void ma ...
- java基础笔记(8)----接口
接口 是特殊的抽象类,纯抽象类---所有方法都是抽象方法 接口和抽象类的区别: 相同点: 编译后,会分别生成对应的.class文件 都不能创建对象(实例化),但是可以生成引用(使用多态) 不同点: 抽 ...