描述,将表单的数据映射到实体中,支持级联属性。
控制器:
/** * 获取表单提交过来的数据到实体中 * 路径: home/testPojo * @param user * @return */ @RequestMapping("/testPojo") public String testPojo(User user){ System.out.println("user=" + user); return "home_index"; }
User.java
package com.shuoeasy.springmvc; public class User { private int uid; private String uname; // 级联属性 private Addres addres; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public Addres getAddres() { return addres; } public void setAddres(Addres addres) { this.addres = addres; } @Override public String toString() { return "User [uid=" + uid + ", uname=" + uname + ", addres=" + addres + "]"; } }
Addres.java:
package com.shuoeasy.springmvc; public class Addres { // 省 private String provice; // 市 private String city; public String getProvice() { return provice; } public void setProvice(String provice) { this.provice = provice; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } @Override public String toString() { return "Addres [provice=" + provice + ", city=" + city + "]"; } }
from.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <form action="home/testPojo" method="post" target="_blank"> uname:<input type="text" name="uname" value="ZhangSan"/> <br/> provice(级联):<input type="text" name="addres.provice" value="GuangXi"/> <br/> city(级联):<input type="text" name="addres.city" value="NanNing"/> <br/> <input type="submit" value="提交" /> </form> <br/> </body> </html>
控制台输出:
user=User [uid=0, uname=ZhangSan, addres=Addres [provice=GuangXi, city=NanNing]]