springMVC数据绑定

步骤
1、springMVC将servletRequest对象传递给databinder
2、将处理方法的入参对象传递给databinder
3、databinder调用conversionservice组件进行数据类型转换、数据格式化等工作,并将servletrequest对象中的消息填充到参数对象中
3、调用validator组件对已经绑定了请求消息数据的参数对象进行数据合法性校验
4、校验完成后会生成数据绑定结果bindingrequest对象,springmvc会将bindingrequest对象中的内容赋给处理方法的相应参数

注意
在使用包装POJO类型数据绑定时,前端请求的参数名编写必须符合以下两种情况:
1、如果查询条件参数是包装类的直接基本属性,参数名就直接用对应的属性名
2、如果查询条件参数是包装类中pojo的子属性,参数名就必须为【对象.属性】,其中【对象】要和包装pojo中的对象属性名称一致,【属性】要和包装pojo中的对象子属性一致

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>chapter12</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置编码过滤器 -->
  <filter>
  	<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
 <servlet>
  	<!-- 配置前端过滤器 -->
  	<servlet-name>dispatcher</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>dispatcher</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

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: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-4.2.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-4.2.xsd">
       <mvc:annotation-driven></mvc:annotation-driven>
        <!-- 指定需要扫描的包 -->
        <context:component-scan base-package="com.ssm.controller"></context:component-scan>
        <!-- 处理器映射,将处理器handle的name作为URL进行查找 -->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
        
        <!-- 视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        	<!-- 设置前缀,可以将view路径简化 -->
        	<property name="prefix" value="/WEB-INF/jsp/"></property>
        	<!-- 设置后缀 -->
        	<property name="suffix" value=".jsp"></property>
        </bean>
</beans>

Banji.java

package com.ssm.po;

public class Banji {
	private Integer banji_id;
	private String banji_name;
	public Integer getBanji_id() {
		return banji_id;
	}
	public void setBanji_id(Integer banji_id) {
		this.banji_id = banji_id;
	}
	public String getBanji_name() {
		return banji_name;
	}
	public void setBanji_name(String banji_name) {
		this.banji_name = banji_name;
	}
	@Override
	public String toString() {
		return "Banji [banji_id=" + banji_id + ", banji_name=" + banji_name + "]";
	}
	
}

Student.java

package com.ssm.po;

public class Student {
	private Integer stu_id;
	private String stu_name;
	private Banji banji;
	public Integer getStu_id() {
		return stu_id;
	}
	public void setStu_id(Integer stu_id) {
		this.stu_id = stu_id;
	}
	public String getStu_name() {
		return stu_name;
	}
	public void setStu_name(String stu_name) {
		this.stu_name = stu_name;
	}
	public Banji getBanji() {
		return banji;
	}
	public void setBanji(Banji banji) {
		this.banji = banji;
	}
	@Override
	public String toString() {
		return "Student [stu_id=" + stu_id + ", stu_name=" + stu_name + ", banji=" + banji + "]";
	}
	
}

UserController.java

package com.ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ssm.po.User;
import com.ssm.vo.UserVo;

@Controller
public class UserController {
	//绑定默认数据类型
	@RequestMapping(value="/selectUser")
	public String selectUser(HttpServletRequest request) {
		String parameter = request.getParameter("id");
		System.out.println("id="+parameter);
		return "success";
	}
	//绑定简单数据类型
	@RequestMapping(value="/selectUser2")
	public String selectUser2(@RequestParam(value="User_id")Integer id) {
		System.out.println("id="+id);
		return "success";
	}
	
	//POJO类型数据绑定
	//向注册页面跳转
	@RequestMapping("/toregister")
	public String toRgister() {
		return "register";
	}
	
	@RequestMapping("/registerUser")
	public String registerUser(User user) {
		String username = user.getUsername();
		String password = user.getPassword();
		System.out.println("username="+username);
		System.out.println("password="+password);
		return "success";
		
	}
	
	//绑定集合
	//向用户批量修改页面跳转
	@RequestMapping("/toUserEdit")
	public String toUserEdit() {
		return "useredit";
	}
	//接收批量修改用户的方法
	@RequestMapping("/userEdit")
	public String userEdit(UserVo userlist) {
		List<User> users = userlist.getUsers();
		if (users!=null) {
			for (User user : users) {
				Integer id = user.getId();
				String username = user.getUsername();
				System.out.println("删除id="+id+",username="+username);
			}
		} else {
			System.out.println("集合为空");
		}
		return "success";
	}
}

CourseController.java

package com.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * 绑定数组
 * @author Administrator
 *
 */
@Controller
public class CourseController {
	@RequestMapping("/toCourse")
	public String toCourse() {
		return "course";
	}
	@RequestMapping("/deleteCourse")
	public String deleteCourse(Integer[]ids) {
		if(ids!=null) {
			for (Integer integer : ids) {
				System.out.println("删除了id为"+integer+"的课程");
			}
		}else {
			System.out.println("ids=null");
		}
		return "success";
	}
}

StudentController.java

package com.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.po.Student;
/*
 * 帮定包装pojo
 */
@Controller
public class StudentController {
	@RequestMapping("/tofindStudentWithBanji")
	public String tofindStudentWithBanji() {
		return "student";
	}
	@RequestMapping("/findStudentWithBanji")
	public String findStudentWithBanji(Student stu) {
		Integer stu_id = stu.getStu_id();
		String banji_name = stu.getBanji().getBanji_name();
		System.out.println("学生编号:"+stu_id);
		System.out.println("班级名称:"+banji_name);
		return "success";
	}
}

UserVo.java

package com.ssm.vo;

import java.util.List;

import com.ssm.po.User;

public class UserVo {
	private List<User> users;

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
	
}

course.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/userEdit" method="post">
		<table>
			<tr>
				<td>选择</td>
				<td>用户名</td>
			</tr>
			<tr>
				<td><input name="users[0].id" value="1" type="checkbox"></td>
				<td><input name="users[0].username" value="zhangsan"  type="text"></td>
			</tr>
			<tr>
				<td><input name="users[1].id" value="2" type="checkbox"></td>
				<td><input name="users[1].username" value="lisi"  type="text"></td>
			</tr>
			
		</table>
		<input type="submit" value="修改">
	</form>
</body>
</html>

register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/registerUser" method="post">
		用户名:<input type="text" name="username"><br>
		密码:<input type="password" name="password"><br>
		<input type="submit" value="注册">
	</form>
</body>
</html>

student.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/findStudentWithBanji" method="post">
		学生编号:<input type="text" name="stu_id"><br>
		班级名称:<input type="text" name="banji.banji_name"><br>
		<input type="submit" value="查询">
	</form>
</body>
</html>

useredit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/userEdit" method="post">
		<table>
			<tr>
				<td>选择</td>
				<td>用户名</td>
			</tr>
			<tr>
				<td><input name="users[0].id" value="1" type="checkbox"></td>
				<td><input name="users[0].username" value="zhangsan"  type="text"></td>
			</tr>
			<tr>
				<td><input name="users[1].id" value="2" type="checkbox"></td>
				<td><input name="users[1].username" value="lisi"  type="text"></td>
			</tr>
			
		</table>
		<input type="submit" value="修改">
	</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	ok,执行成功
</body>
</html>

绑定默认数据类型

//绑定默认数据类型
	@RequestMapping(value="/selectUser")
	public String selectUser(HttpServletRequest request) {
		String parameter = request.getParameter("id");
		System.out.println("id="+parameter);
		return "success";
	}

地址栏输入:http://localhost:8080/chapter12/selectUser?id=1

绑定简单数据类型

//绑定简单数据类型
	@RequestMapping(value="/selectUser2")
	public String selectUser2(@RequestParam(value="User_id")Integer id) {
		System.out.println("id="+id);
		return "success";
	}

地址栏输入:http://localhost:8080/chapter12/selectUser2?User_id=1

绑定POJO类型

//POJO类型数据绑定
	//向注册页面跳转
	@RequestMapping("/toregister")
	public String toRgister() {
		return "register";
	}
	
	@RequestMapping("/registerUser")
	public String registerUser(User user) {
		String username = user.getUsername();
		String password = user.getPassword();
		System.out.println("username="+username);
		System.out.println("password="+password);
		return "success";
		
	}

地址栏输入:http://localhost:8080/chapter12/toregister

绑定包装POJO

/*
 * 帮定包装pojo
 */
@Controller
public class StudentController {
	@RequestMapping("/tofindStudentWithBanji")
	public String tofindStudentWithBanji() {
		return "student";
	}
	@RequestMapping("/findStudentWithBanji")
	public String findStudentWithBanji(Student stu) {
		Integer stu_id = stu.getStu_id();
		String banji_name = stu.getBanji().getBanji_name();
		System.out.println("学生编号:"+stu_id);
		System.out.println("班级名称:"+banji_name);
		return "success";
	}
}

地址栏输入:http://localhost:8080/chapter12/tofindStudentWithBanji

绑定数组

/**
 * 绑定数组
 * @author Administrator
 *
 */
@Controller
public class CourseController {
	@RequestMapping("/toCourse")
	public String toCourse() {
		return "course";
	}
	@RequestMapping("/deleteCourse")
	public String deleteCourse(Integer[]ids) {
		if(ids!=null) {
			for (Integer integer : ids) {
				System.out.println("删除了id为"+integer+"的课程");
			}
		}else {
			System.out.println("ids=null");
		}
		return "success";
	}

地址栏输入:http://localhost:8080/chapter12/toCourse

绑定集合

//绑定集合
	//向用户批量修改页面跳转
	@RequestMapping("/toUserEdit")
	public String toUserEdit() {
		return "useredit";
	}
	//接收批量修改用户的方法
	@RequestMapping("/userEdit")
	public String userEdit(UserVo userlist) {
		List<User> users = userlist.getUsers();
		if (users!=null) {
			for (User user : users) {
				Integer id = user.getId();
				String username = user.getUsername();
				System.out.println("删除id="+id+",username="+username);
			}
		} else {
			System.out.println("集合为空");
		}
		return "success";
	}

地址栏输入:http://localhost:8080/chapter12/toUserEdit

日期转换

https://blog.csdn.net/mybook201314/article/details/54232480
数据绑定的日期转换

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值