1、Spring MVC 会按请求参数名和 POJO 属性名进行自动匹
配,自动为该对象填充属性值。支持级联属性
2、使用 POJO 对象能绑定请求参数值,方便视图和handler之间数据传递
3、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>SpringMVC_3_POJO</display-name>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 配置自动扫描包 -->
<context:component-scan base-package="com.test.springmvc"></context:component-scan>
<!-- 配置视图解析器,把handler返回值解析为实际视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5、Handler.java
package com.test.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.test.springmvc.dao.Info;
@RequestMapping("/test")
@Controller
public class Handler {
//测试POJO,SpringMVC会根据请求参数名和POJO类的属性名自动匹配为对应对象填充值
//注意:支持级联操作
@RequestMapping("/testPOJO")
public String testPOJO(Info info){
System.out.println("testPOJO info:"+info);
return "result";
}
}
6、Info.java
package com.test.springmvc.dao;
public class Info {
Integer infoId;
String infoName;
String infoDesc;
Detail detail;
public Integer getInfoId() {
return infoId;
}
public void setInfoId(Integer infoId) {
this.infoId = infoId;
}
public String getInfoName() {
return infoName;
}
public void setInfoName(String infoName) {
this.infoName = infoName;
}
public String getInfoDesc() {
return infoDesc;
}
public void setInfoDesc(String infoDesc) {
this.infoDesc = infoDesc;
}
public Detail getDetail() {
return detail;
}
public void setDetail(Detail detail) {
this.detail = detail;
}
@Override
public String toString() {
return "Info [infoId=" + infoId + ", infoName=" + infoName + ", infoDesc=" + infoDesc + ", detail=" + detail
+ "]";
}
}
7、Detail.java
package com.test.springmvc.dao;
public class Detail {
Integer detailId;
String detailName;
public Integer getDetailId() {
return detailId;
}
public void setDetailId(Integer detailId) {
this.detailId = detailId;
}
public String getDetailName() {
return detailName;
}
public void setDetailName(String detailName) {
this.detailName = detailName;
}
@Override
public String toString() {
return "Detail [detailId=" + detailId + ", detailName=" + detailName + "]";
}
}
8、index.jsp
<%@ 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>Insert title here</title>
</head>
<body>
<form action="test/testPOJO" method="post">
<input type="text" name="infoId" value="1">
<input type="text" name="infoName" value="info">
<input type="text" name="infoDesc" value="desc">
<input type="text" name="detail.detailId" value="01">
<input type="text" name="detail.detailName" value="detail">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
9、result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
result page
</body>
</html>
10、注意事项
(1)html的form表单中的参数名,需要跟bean中的参数名一致
(2)html的form表单中的参数名,如果需要使用级联操作的时候可以这样写 如:detail.detailId (代表Info Bean中的Detail Bean对象里面的detailId参数)
11、项目目录
扫描二维码关注公众号,回复:
2745038 查看本文章
12、demo
https://download.csdn.net/download/qq_22778717/10599056