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对象绑定请求参数值的更多相关文章

  1. 【SpringMVC】SpringMVC系列7之POJO 对象绑定请求参数值

      7.POJO 对象绑定请求参数值 7.1.概述 Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值.而且支持级联属性.如:dept.deptId.dept ...

  2. SpringMVC系列(四)使用 POJO 对象绑定请求参数值

    在实际开发中如果参数太多就不能使用@RequestParam去一个一个的映射了,需要定义一个实体参数对象(POJO)来映射请求参数.Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配 ...

  3. SpringMVC学习 -- 使用 POJO 对象绑定请求参数值

    Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配 , 自动为该对象填充属性值 , 支持级联属性.如:address.province. package com.itdoc.spri ...

  4. SpringMVC之使用 POJO 对象绑定请求参数值

    Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值.支持级联属性.如:dept.deptId.dept.address.tel 等 示例: User实体类 p ...

  5. 使用 POJO 对象绑定请求参数

    概述 Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值并且支持级联属性.这一特性在日常开发过程中使用频率比较高,开发效率也高,本文主要对 POJO 对象绑定 ...

  6. 008 使用POJO对象绑定请求参数

    1.介绍 2.Person.java package com.spring.bean; public class Person { private String username; private S ...

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

    在处理方法入参使用@RequestParam可以把请求参数传递给请求方法,@RequestParam包含的属性值: --- value :参数名称 --- required :是否必须,默认为true ...

  8. SpringMVC(七) RequestMapping 路径中带占位符的URL

    使用方法:在@RequestMapping("/delete/{id}")中,通过{id}带入pathvariable,然后在方法中,通过@PathVariable("变 ...

  9. 数据绑定-POJO对象绑定参数

    测试: 效果:

随机推荐

  1. 使用python读取word,写入execl

    word里面有2张表,需要找到第二张表,并写入execl中: 代码如下: #coding:utf-8 import os from docx import Document import win32c ...

  2. day01的那些事

    代码之道 路漫漫其修远兮,吾将上下而求索

  3. 从Socket入门到BIO,NIO,multiplexing,AIO

    Socket入门 最简单的Server端读取Client端内容的demo public class Server { public static void main(String [] args) t ...

  4. Eclipse项目出现红色叹号的解决办法

    以前的项目今天打开突然出现了红色的叹号,对于强迫症的患者简直忍不了,出现红色叹号的原因都是jar包出现问题导致的,如果是代码错误早就是一个大红叉了- 打开项目就可以发现,找不到哪里出问题了,代码和js ...

  5. spy++捕获窗口消息

    打开spy++,窗口截图如下,点击窗口搜索按钮(红框标识) ,如果找不到对应的窗口,鼠标右键刷新即可. 鼠标左键点击窗口搜索图标,按住不放,拖到需要抓取消息的窗口上: spy++会自动在列表中高亮定位 ...

  6. delete与delete[]的区别

    一直对C++中的delete和delete[]的区别不甚了解,今天遇到了,上网查了一下,得出了结论.做个备份,以免丢失. C++告诉我们在回收用 new 分配的单个对象的内存空间的时候用 delete ...

  7. Vue常用开源项目汇总

    前言:Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易于上手,还 ...

  8. Android权限Uri.parse的几种用法(转载)

    1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = new Intent(Intent.ACTI ...

  9. 一周Maven框架学习随笔

    第一次写博客,可能写得不是很好,但是希望自己持之以恒,以后会更好.也希望通过写博客记录随笔,让自己本身有所收获. 下面是今天的maven总结: maven个人理解中是Maven项目对象模型(POM), ...

  10. 关于php日期前置是否有0

    例如:2018-01-04,这个日期和月份前置是有0 如果不想有0,date( 'y-n-j',time() ):默认的是date( 'y-m-d',time() ),这个日期和月份前置是有0. da ...