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>
目录
相关文章
|
5天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用
|
12天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
23 0
|
14天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
18 1
|
14天前
|
JSON 数据格式 Python
Python处理JSON数据
【4月更文挑战第30天】该内容介绍了Python处理JSON数据的三个方法:1)使用`json.loads()`尝试解析字符串以验证其是否为有效JSON,通过捕获`JSONDecodeError`异常判断有效性;2)通过`json.dumps()`的`indent`参数格式化输出JSON数据,使其更易读;3)处理JSON中的日期,利用`dateutil`库将日期转换为字符串进行序列化和反序列化。
23 4
|
18天前
|
存储 JSON 数据处理
|
5月前
|
设计模式 前端开发 JavaScript
Spring MVC(一)【什么是Spring MVC】
Spring MVC(一)【什么是Spring MVC】
|
2月前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
16 0
|
7月前
|
前端开发 Java Go
Spring MVC 和 Spring Boot 的区别
Spring MVC 和 Spring Boot 的区别
114 0
|
10月前
|
Java Spring
springmvc中spring提供的中文乱码解决问题
可以解决浏览器的乱码问题
44 0
|
5月前
|
设计模式 前端开发 Java
Spring Boot之Spring MVC的工作原理 以及使用eclipse开发Spring MVC的Web应用实战(附源码)
Spring Boot之Spring MVC的工作原理 以及使用eclipse开发Spring MVC的Web应用实战(附源码)
50 0