Spring MVC,绑定包装POJO类型

所谓包装POJO,就是一在个POJO中包含另一个简单POJO。示范:用户的证件信息。

一、假定基本环境已经具备

参考:Spring MVC,绑定默认数据

二、创建POJO类(两个)

package com.itheima.po;
/**
 * 个人持久化类
 */
public class Person {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
    private IdCard card;  //个人关联的证件
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public IdCard getCard() {
        return card;
    }
    public void setCard(IdCard card) {
        this.card = card;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", "
                + "age=" + age + ", sex=" + sex + ", card=" + card + "]";
    }
}
package com.itheima.po;
/**
 * 证件持久化类
 */
public class IdCard {
    private Integer id;
    private String code;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    @Override
    public String toString() {
        return "IdCard [id=" + id + ", code=" + code + "]";
    }
}

三、创建前台表单页面form3.jsp,位于:mvc目录

<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>提交POJO类型2</title>
</head>
<body>
<form action="../hello/pojoParameter2" method="post">
        用户id:<input type="text" name="id" value="2"></br>
        用户名:<input type="text" name="name" value="Andy"></br>
        年龄:<input type="text" name="age" value="30"></br>
        性别:<input type="text" name="sex" value="男"></br>
        证件id:<input type="text" name="card.id" value="1234"></br>
        证件编码:<input type="text" name="card.code" value="321001198701019012"></br>
        <input type="submit" value="提交">
</form>
</body>
</html> 

注意:【证件id】和【证件编码】中name的写法,它的前缀不是IdCard的类名,而是Person类中IdCard类对应的变量名

四、创建后台控制器类

package com.itheima.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.itheima.po.Person;

@Controller
@RequestMapping(value = "/hello")
public class POJOController2 {
    @RequestMapping("/pojoParameter2")
    public String pojoParameter(Person person){
        System.out.println(person);
        
        return "success";
    }
}

五、测试,打开网页:http://localhost:8080/ssm/mvc/form3.jsp

 提交,后台显示:

 

posted @ 2021-10-04 11:02  那些年的事儿  阅读(243)  评论(0编辑  收藏  举报