SpringMVC - 数据绑定(基本数据、包装类、数组、对象、集合:List、Set、Map、Json)(一)

简介: SpringMVC - 数据绑定(基本数据、包装类、数组、对象、集合:List、Set、Map、Json)(一)

image.png

image.png


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <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.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>
</web-app>
<?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.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <context:component-scan base-package="com.imooc"></context:component-scan>
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/"></property>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
package com.imooc.entity;
public class Author {
    private int id;
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
}
package com.imooc.entity;
public class Course {
    private int id;
    private String name;
    private double price;
    private Author author;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public Author getAuthor() {
        return author;
    }
    public void setAuthor(Author author) {
        this.author = author;
    }
}
package com.imooc.entity;
import java.util.HashSet;
import java.util.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());
    }
}
package com.imooc.entity;
import java.util.List;
public class CourseList {
    private List<Course> courses;
    public List<Course> getCourses() {
        return courses;
    }
    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }
}
package com.imooc.entity;
import java.util.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;
    }
}
package com.imooc.dao;
import com.imooc.entity.Course;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@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();
    }
}
package com.imooc.controller;
import com.imooc.dao.CourseDAO;
import com.imooc.entity.Course;
import com.imooc.entity.CourseList;
import com.imooc.entity.CourseMap;
import com.imooc.entity.CourseSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class DataBindController {
    @Autowired
    private CourseDAO courseDAO;
    @RequestMapping(value = "/baseType")
    @ResponseBody
    public String baseType(@RequestParam(value = "id") int id){
        return "id:"+id;
    }
    @RequestMapping(value = "/packageType")
    @ResponseBody
    public String packageType(@RequestParam(value = "id") Integer id){
        return "id:"+id;
    }
    @RequestMapping(value = "/arrayType")
    @ResponseBody
    public String arrayType(String[] name){
        StringBuffer sbf = new StringBuffer();
        for (String item:name){
            sbf.append(item).append(" ");
        }
        return sbf.toString();
    }
    @RequestMapping(value = "/pojoType")
    public ModelAndView pojoType(Course course){
        courseDAO.add(course);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("courses",courseDAO.getAll());
        return modelAndView;
    }
    @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;
    }
    @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;
    }
    @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;
    }
    @RequestMapping(value = "/jsonType")
    @ResponseBody
    public  Course jsonType(@RequestBody  Course course){
        course.setPrice(course.getPrice()+100);
        return course;
    }
}
// addPojo.jsp 核心代码
<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>
// addList.jsp 核心代码
<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>
// addMap.jsp 核心代码
<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>
// addSet.jsp 核心代码
<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>
// json.jsp 核心代码
<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>
// index.jsp 核心代码
<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>&nbsp;&nbsp;
            <button class="btn btn-danger btn-sm delete_btn">
                <span class="glyphicon glyphicon-trash">删除</span>
            </button>
        </td>
    </tr>
</c:forEach>
目录
相关文章
|
3天前
|
JSON 前端开发 JavaScript
json字符串如何转为list对象?
json字符串如何转为list对象?
14 7
|
3天前
|
JSON JavaScript 前端开发
js如何格式化一个JSON对象?
js如何格式化一个JSON对象?
12 3
|
8天前
|
NoSQL Java Redis
List集合按照由小到大排序或者由大到小排序
List集合按照由小到大排序或者由大到小排序
10 3
|
9天前
|
XML JSON JavaScript
JSON对象的stringify()和parse()方法使用
本文阐述了JSON对象的`stringify()`和`parse()`方法的用法,包括如何将JavaScript对象转换为JSON字符串,以及如何将JSON字符串解析回JavaScript对象,并讨论了转换过程中需要注意的事项。
JSON对象的stringify()和parse()方法使用
|
12天前
|
JSON 前端开发 中间件
React读取properties配置文件转化为json对象并使用在url地址中
本文介绍了如何在React项目中读取properties配置文件,将其内容转化为JSON对象,并在请求URL地址时使用这些配置。文章详细说明了异步读取文件、处理字符串转换为JSON对象的过程,并提供了一个封装函数,用于在发起请求前动态生成配置化的URL地址。
27 1
|
4月前
|
Dart
Dart之集合详解(List、Set、Map)
Dart之集合详解(List、Set、Map)
|
10天前
|
Go 定位技术 索引
Go 语言Map(集合) | 19
Go 语言Map(集合) | 19
|
9天前
|
存储 前端开发 API
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
该文章详细介绍了ES6中Set和Map数据结构的特性和使用方法,并探讨了它们在前端开发中的具体应用,包括如何利用这些数据结构来解决常见的编程问题。
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
|
2月前
|
存储 安全 Java
java集合框架复习----(4)Map、List、set
这篇文章是Java集合框架的复习总结,重点介绍了Map集合的特点和HashMap的使用,以及Collections工具类的使用示例,同时回顾了List、Set和Map集合的概念和特点,以及Collection工具类的作用。
java集合框架复习----(4)Map、List、set
|
2月前
|
Java
【Java集合类面试二十二】、Map和Set有什么区别?
该CSDN博客文章讨论了Map和Set的区别,但提供的内容摘要并未直接解释这两种集合类型的差异。通常,Map是一种键值对集合,提供通过键快速检索值的能力,而Set是一个不允许重复元素的集合。
下一篇
无影云桌面