Spring MVC List集合参数封装

在之前Spring MVC使用包装Pojo类型一文中,我们是一个Address对象来接收一个地址信息,如果有多个地址信息怎么呢?这时我们可以使用List集合来封装。

1 设计表单

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>一点教程网 - www.yiidian.com</title>
</head>
<body>
<h2>List集合类型参数封装</h2>
<form action="/param.do" method="post">
    用户名:<input type="text" name="username"><br>
    年龄:<input type="text" name="age"><br>
    省份1:<input type="text" name="address[0].province"><br>
    城市1:<input type="text" name="address[0].city"><br>
    省份2:<input type="text" name="address[1].province"><br>
    城市2:<input type="text" name="address[1].city"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

注意:这里的name比较特殊:address[0].province,代表给User对象->List<Address>集合->第一个Address对象的province属性赋值

2 设计Pojo对象

Address对象:

package com.yiidian.domain;

/**
 * 封装用户的地址信息
 * 一点教程网 - www.yiidian.com
 */
public class Address {
    private String province;
    private String city;

    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;
    }

    @Override
    public String toString() {
        return "Address{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}

User对象:

package com.yiidian.domain;

import java.util.List;

/**
 * 用于封装表单数据
 * 一点教程网 - www.yiidian.com
 */
public class User {
    private String username;
    private Integer age;

    private List<Address> address;//这里使用List集合接收表单多个地址信息

    public List<Address> getAddress() {
        return address;
    }
    public void setAddress(List<Address> address) {
        this.address = address;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

}

3 编写Controller

package com.yiidian.controller;
import com.yiidian.domain.Address;
import com.yiidian.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * List集合参数封装
 * 一点教程网 - www.yiidian.com
 */
@Controller
public class ParamController {

    @RequestMapping("/param.do")
    public String save(User user){
        System.out.println("用户名:"+user.getUsername());
        System.out.println("年龄:"+user.getAge());
        //遍历所有地址信息
        for(Address addr:user.getAddress()){
            System.out.println(addr);
        }
        return "success";
    }
}

4 springmvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 1.扫描Controller的包-->
    <context:component-scan base-package="com.yiidian.controller"/>

    <!-- 2.配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 2.1 页面前缀 -->
        <property name="prefix" value="/pages/"/>
        <!-- 2.2 页面后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 3.创建处理器适配器和处理器映射器-->
    <mvc:annotation-driven/>
</beans>

5 运行测试

控制台输出:

源码下载:https://pan.baidu.com/s/1a7nIg7lSKKuwLb3gGQ8G4w

热门文章

优秀文章