点击返回一级目录
点击返回二级目录
利用Map集合也可以很方便的将一组数据封装起来
5.5.1设计表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>测试封装Map</title>
</head>
<body>
<h2>Map集合类型参数封装</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['a1'].province"><br>
城市1:<input type="text" name="address['a1'].city"><br>
省份2:<input type="text" name="address['a2'].province"><br>
城市2:<input type="text" name="address['a2'].city"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
注意:这里的address[‘a1’].city,a1是赋值给Map的key,city是赋值给Address的city属性
5.5.2设计Address对象
package com.bean;
public class AddressBean {
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 "AddressBean{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
}
5.5.3设计User对象
package com.bean;
import java.util.Map;
public class UserBean {
private String username;
private Integer age;
private Map<String,AddressBean> address;
public Map<String, AddressBean> getAddress() {
return address;
}
public void setAddress(Map<String, AddressBean> 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;
}
}
5.5.4设计Controller对象
package com.controller;
import com.bean.AddressBean;
import com.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
public class MapController {
@RequestMapping("/map")
public String map(UserBean userBean){
System.out.println("用户名:"+userBean.getUsername());
System.out.println("年龄:"+userBean.getAge());
//遍历所有地址信息
Map<String, AddressBean> address = userBean.getAddress();
for(Map.Entry entry:address.entrySet()){
System.out.println(entry.getKey()+"--"+entry.getValue());
}
return "success";
}
}
5.5.5编写springmvc配置
<?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.controller"/>
<!-- 2.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 2.1 页面前缀 -->
<property name="prefix" value="/"/>
<!-- 2.2 页面后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 3.开启mvc注解驱动-->
<mvc:annotation-driven/>
<!-- 4.开启静态资源访问-->
<mvc:default-servlet-handler/>
</beans>