springmvc绑定包装pojo
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
新建maven项目
导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shrimpking</groupId>
<artifactId>springtest13</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>p01</module>
<module>p02</module>
<module>p03</module>
<module>p04</module>
<module>p05</module>
<module>p06</module>
<module>p07</module>
<module>p08</module>
<module>p09</module>
</modules>
<dependencies>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version> 1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
BanJi.java
package com.shrimpking.pojo;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/7/15 10:48
*/
public class BanJi
{
private Integer id;
private String banJiName;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getBanJiName()
{
return banJiName;
}
public void setBanJiName(String banJiName)
{
this.banJiName = banJiName;
}
@Override
public String toString()
{
return "BanJi{" + "id=" + id + ", banJiName='" + banJiName + '\'' + '}';
}
}
Student.java
package com.shrimpking.pojo;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/7/15 10:53
*/
public class Student
{
private Integer id;
private String stuName;
private String sex;
private BanJi banJi;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public BanJi getBanJi()
{
return banJi;
}
public void setBanJi(BanJi banJi)
{
this.banJi = banJi;
}
@Override
public String toString()
{
return "Student{" + "id=" + id + ", stuName='" + stuName + '\'' + ", sex='" + sex + '\'' + ", banJi=" + banJi + '}';
}
}
StudentController.java
package com.shrimpking.controller;
import com.shrimpking.pojo.BanJi;
import com.shrimpking.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/7/15 10:56
* 注意
* 在使用包装POJO类型数据绑定时,
* 前端请求的参数名编写必须符合以下两种情况。
* 1 如果查询条件参数是包装类的直接基本属性,
* 参数名就直接用对应的属性名,如上述代码中的stuName。
* 2 如果查询条件参数是包装类中POJO的子属性,
* 参数名就必须为【对象.属性】,
* 其中【对象】要和包装POJO中的对象属性名称一致,
* 【属性】要和包装POJO中的对象子属性一致,
* 如上述代码中的banJi.banJiName。
*/
@Controller
public class StudentController
{
//向学生查询页面跳转
@RequestMapping("/tofind")
public String toFindStudentWithBanJi()
{
return "student";
}
//查询学生和班级信息
@RequestMapping("/findStudent")
public String findStudentWithBanJi(Student student)
{
Integer stuId = student.getId();
String stuName = student.getStuName();
String stuSex = student.getSex();
BanJi banJi = student.getBanJi();
Integer banJiId = banJi.getId();
String banJiName = banJi.getBanJiName();
System.out.println("student:" + student.toString());
System.out.println("banji:" + banJi.toString());
return "success";
}
}
springmvc-config.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"
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.xsd">
<!-- 扫描包 -->
<context:component-scan base-package="com.shrimpking.controller"/>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码 -->
<filter>
<filter-name>Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
student.jsp
<%--
Created by IntelliJ IDEA.
User: user1
Date: 2023/7/15
Time: 11:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生查询</title>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/findStudent">
学生编号: <input type="text" name="id"><br>
学生姓名: <input type="text" name="stuName"><br>
学生性别: <input type="text" name="sex"><br>
班级编号: <input type="text" name="banJi.id"><br>
所属班级: <input type="text" name="banJi.banJiName"><br>
<input type="submit" value="查询">
</form>
</body>
</html>
success.jsp
<%--
Created by IntelliJ IDEA.
User: user1
Date: 2023/7/15
Time: 11:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h2>ok,查询成功</h2>
</body>
</html>
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |