什么是数据绑定?
将HTTP请求中的参数绑定到Handler业务方法的形参。
常用的数据绑定类型
1.基本数据类型
@ResponseBody的作用其实是将java对象转为json格式的数据。
@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。
注意:在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。
@RequestParam:将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)
语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)
- value:参数名
- required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。
- defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值
2.包装类
3.数组
4.对象
Author.java
public class Author {
private int id;
private String name;
//get和set
}
Course.java
public class Course {
private int id;
private String name;
private double price;
private Author author;
//get和set
}
CourseDAO.java
@Repository
public class CourseDAO {
private Map<Integer,Course> courses = new HashMap<Integer, Course>();
public void add(Course course){
courses.put(course.getId(),course);
}
public Collection<Course> getAll(){
return courses.values();
}
}
add.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{
overflow-x:hidden;
}
#main{
width:1200px;
height:600px;
margin-left:500px;
}
</style>
</head>
<body>
<div id="main">
<!-- 标题 -->
<div class="row">
<div class="col-md-12">
<h1>imooc-添加课程</h1>
</div>
</div>
<form class="form-horizontal" role="form" action="pojoType" method="post">
<div class="form-group">
<label class="col-sm-1 control-label">课程编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="id" placeholder="请输入课程编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程名称</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="name" placeholder="请输入课程名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程价格</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="price" placeholder="请输入课程价格">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="author.id" placeholder="请输入讲师编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师姓名</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="author.name" placeholder="请输入讲师姓名">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-3">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
</div>
</body>
</html>
index.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>课程列表</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<!-- 标题 -->
<div class="row">
<div class="col-md-12">
<h1>imooc-课程管理</h1>
</div>
</div>
<!-- 显示表格数据 -->
<div class="row">
<div class="col-md-12">
<table class="table table-hover" id="emps_table">
<thead>
<tr>
<th>
<input type="checkbox" id="check_all"/>
</th>
<th>编号</th>
<th>课程名</th>
<th>价格</th>
<th>讲师</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${courses}" var="course">
<tr>
<td><input type='checkbox' class='check_item'/></td>
<td>${course.id}</td>
<td>${course.name}</td>
<td>${course.price}</td>
<td>${course.author.name}</td>
<td>
<button class="btn btn-primary btn-sm edit_btn">
<span class="glyphicon glyphicon-pencil">编辑</span>
</button>
<button class="btn btn-danger btn-sm delete_btn">
<span class="glyphicon glyphicon-trash">删除</span>
</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
5.集合(List、Set、Map)
List
CourseList.java
public class CourseList {
private List<Course> courses;
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
}
addList.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{
overflow-x:hidden;
}
#main{
width:1200px;
height:600px;
margin-left:500px;
}
</style>
</head>
<body>
<div id="main">
<!-- 标题 -->
<div class="row">
<div class="col-md-12">
<h1>imooc-添加课程</h1>
</div>
</div>
<form class="form-horizontal" role="form" action="listType" method="post">
<div class="form-group">
<label class="col-sm-1 control-label">课程1编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[0].id" placeholder="请输入课程编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程1名称</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[0].name" placeholder="请输入课程名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程1价格</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[0].price" placeholder="请输入课程价格">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[0].author.id" placeholder="请输入讲师编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师姓名</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[0].author.name" placeholder="请输入讲师姓名">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程2编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[1].id" placeholder="请输入课程编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程2名称</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[1].name" placeholder="请输入课程名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程2价格</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[1].price" placeholder="请输入课程价格">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[1].author.id" placeholder="请输入讲师编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师姓名</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[1].author.name" placeholder="请输入讲师姓名">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-3">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
</div>
</body>
</html>
@RequestMapping(value = "/listType")
public ModelAndView listType(CourseList courseList){
for(Course course:courseList.getCourses()){
courseDAO.add(course);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("courses",courseDAO.getAll());
return modelAndView;
}
Set
public class CourseSet {
private Set<Course> courses = new HashSet<Course>();
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
public Set<Course> getCourses() {
return courses;
}
public CourseSet(){
courses.add(new Course());
courses.add(new Course());
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{
overflow-x:hidden;
}
#main{
width:1200px;
height:600px;
margin-left:500px;
}
</style>
</head>
<body>
<div id="main">
<!-- 标题 -->
<div class="row">
<div class="col-md-12">
<h1>imooc-添加课程</h1>
</div>
</div>
<form class="form-horizontal" role="form" action="setType" method="post">
<div class="form-group">
<label class="col-sm-1 control-label">课程1编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[0].id" placeholder="请输入课程编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程1名称</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[0].name" placeholder="请输入课程名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程1价格</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[0].price" placeholder="请输入课程价格">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[0].author.id" placeholder="请输入讲师编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师姓名</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[0].author.name" placeholder="请输入讲师姓名">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程2编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[1].id" placeholder="请输入课程编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程2名称</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[1].name" placeholder="请输入课程名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程2价格</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[1].price" placeholder="请输入课程价格">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[1].author.id" placeholder="请输入讲师编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师姓名</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses[1].author.name" placeholder="请输入讲师姓名">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-3">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
</div>
</body>
</html>
@RequestMapping(value = "/setType")
public ModelAndView setType(CourseSet courseSet){
for (Course course:courseSet.getCourses()){
courseDAO.add(course);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("courses",courseDAO.getAll());
return modelAndView;
}
Map
public class CourseMap {
private Map<String,Course> courses;
public void setCourses(Map<String, Course> courses) {
this.courses = courses;
}
public Map<String, Course> getCourses() {
return courses;
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{
overflow-x:hidden;
}
#main{
width:1200px;
height:600px;
margin-left:500px;
}
</style>
</head>
<body>
<div id="main">
<!-- 标题 -->
<div class="row">
<div class="col-md-12">
<h1>imooc-添加课程</h1>
</div>
</div>
<form class="form-horizontal" role="form" action="mapType" method="post">
<div class="form-group">
<label class="col-sm-1 control-label">课程1编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses['one'].id" placeholder="请输入课程编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程1名称</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses['one'].name" placeholder="请输入课程名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程1价格</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses['one'].price" placeholder="请输入课程价格">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses['one'].author.id" placeholder="请输入讲师编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师姓名</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses['one'].author.name" placeholder="请输入讲师姓名">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程2编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses['two'].id" placeholder="请输入课程编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程2名称</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses['two'].name" placeholder="请输入课程名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程2价格</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses['two'].price" placeholder="请输入课程价格">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses['two'].author.id" placeholder="请输入讲师编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">讲师姓名</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="courses['two'].author.name" placeholder="请输入讲师姓名">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-3">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
</div>
</body>
</html>
@RequestMapping(value = "/mapType")
public ModelAndView mapType(CourseMap courseMap){
for(String key:courseMap.getCourses().keySet()){
Course course = courseMap.getCourses().get(key);
courseDAO.add(course);
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("courses",courseDAO.getAll());
return modelAndView;
}
6.JSON
添加依赖
json.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
var course = {
"id":"8",
"name":"SSM框架整合",
"price":"200"
};
$.ajax({
url:"jsonType",
data:JSON.stringify(course),
type:"post",
contentType:"application/json;charse=UTF-8",
dataType:"json",
success:function(data){
alert(data.name+"---"+data.price);
}
})
})
</script>
<body>
</body>
</html>
@RequestMapping(value = "/jsonType")
@ResponseBody
public Course jsonType(@RequestBody Course course){
course.setPrice(course.getPrice()+100);
return course;
}