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>
目录
相关文章
|
30天前
|
存储 分布式计算 NoSQL
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
24 3
|
1月前
|
JSON 前端开发 JavaScript
json字符串如何转为list对象?
json字符串如何转为list对象?
126 7
|
2月前
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
35 5
|
2月前
|
存储 JSON NoSQL
redis基本数据结构(String,Hash,Set,List,SortedSet)【学习笔记】
这篇文章是关于Redis基本数据结构的学习笔记,包括了String、Hash、Set、List和SortedSet的介绍和常用命令。文章解释了每种数据结构的特点和使用场景,并通过命令示例演示了如何在Redis中操作这些数据结构。此外,还提供了一些练习示例,帮助读者更好地理解和应用这些数据结构。
redis基本数据结构(String,Hash,Set,List,SortedSet)【学习笔记】
|
2月前
|
Python
Python量化炒股的获取数据函数— get_billboard_list()
Python量化炒股的获取数据函数— get_billboard_list()
|
2月前
|
Java API 开发者
代码小妙招:用Java轻松获取List交集数据
在Java中获取两个 `List`的交集可以通过 `retainAll`方法和Java 8引入的流操作来实现。使用 `retainAll`方法更为直接,但会修改原始 `List`的内容。而使用流则提供了不修改原始 `List`、更为灵活的处理方式。开发者可以根据具体的需求和场景,选择最适合的方法来实现。了解和掌握这些方法,能够帮助开发者在实际开发中更高效地处理集合相关的问题。
89 1
|
3月前
|
存储 Java 索引
|
Web App开发 JSON JavaScript
第168天:json对象和字符串的相互转换
json对象和字符串的相互转换 1、json对象和字符串的转换 在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法。
1499 0
|
Web App开发 JSON 数据格式
JSON对象与字符串之间的相互转换 - CSDN博客
原文:JSON对象与字符串之间的相互转换 - CSDN博客 [html] view plain copy print?                  JSON对象与字符串之间的相互...
1652 0
|
Web App开发 JSON JavaScript
JSON对象和字符串之间的相互转换
原文:JSON对象和字符串之间的相互转换 比如我有两个变量,我要将a转换成字符串,将b转换成JSON对象: view source print? 1 var a=...
856 0