SpringMVC包装POJO对象类型
程序员文章站
2022-03-24 15:25:55
...
点击返回一级目录
点击返回二级目录
包装参数,就是一个Pojo对象中包含另外一个Pojo对象
5.3.1设计Address对象
package com.bean;
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;
}
}
5.3.2设计User对象
package com.bean;
public class User {
private String username;
private Integer age;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(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;
}
}
5.3.3 设计表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>测试Pojo封装</title>
</head>
<body>
<h2>包装Pojo类型参数封装</h2>
<form action="/param.do" method="post">
用户名:<input type="text" name="username"><br>
年龄:<input type="text" name="age"><br>
省份:<input type="text" name="address.province"><br>
城市:<input type="text" name="address.city"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
5.3.4 设计controller
package com.controller;
import com.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class POJOController {
@RequestMapping("/param.do")
public String save(User user){
System.out.println("用户名:"+user.getUsername());
System.out.println("年龄:"+user.getAge());
System.out.println("省份:"+user.getAddress().getProvince());
System.out.println("城市:"+user.getAddress().getCity());
return "hello";
}
}
5.3.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="/pages/"/>
<!-- 2.2 页面后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 3.创建处理器适配器和处理器映射器-->
<mvc:annotation-driven/>
</beans>
5.3.6测试成功截图
上一篇: 秒杀系统中常见问题及解决方案
推荐阅读
-
017.2 基本数据类型对象包装类
-
springmvc如何将对象类型的数据放入到model对象中
-
java基础之基本数据类型对象包装类,StringBuffer
-
java基础之基本数据类型对象包装类,StringBuffer
-
SpringMvc @RequestParam 使用推荐使用包装类型代替包装类型
-
【Mybatis_POJO包装类型】关于一个实体类装多个实体类如何映射
-
MyBatis中Pojo包装对象及实体类结果集封装简介
-
SpringMVC----将请求参数赋值到POJO对象(8)
-
SpringMVC数据绑定二(自定义数据类型与复杂数据类型对象)
-
后端接口同时接收POJO对象(json)和基本数据类型的情况下,前端如何传值