SpringMVC框架03——数据绑定

一、绑定基本数据类型

在Controller类中添加业务方法:javascript

/** * 绑定基本数据类型 */ @RequestMapping("/baseType") @ResponseBody public String baseType(@RequestParam("id") int id){ return "id="+id; }

 访问:html

二、绑定包装类

Controller类中的业务方法:java

/** * 绑定包装类 */ @RequestMapping("/packageType") @ResponseBody public String packageType(@RequestParam("id") Integer id){ return "id:"+id; }

 访问:jquery

三、绑定数组类型

Controller类中的业务方法:ajax

/** * 绑定数组 */ @RequestMapping("/arrayType") @ResponseBody public String arrayType(String[] name){ StringBuffer stringBuffer = new StringBuffer(); for (String item:name){ stringBuffer.append(item+" "); } return stringBuffer.toString(); }

 访问:spring

四、绑定POJO对象

建立Course类和Author类数据库

public class Course { private int id; private String name; private double price; private Author author; //getter()、setter()
}
public class Author { private int id; private String name; //getter、setter()
}

 建立CourseDao类,模拟数据库存储:json

@Repository public class CourseDao { //模拟数据库存储数据
    private Map<Integer, Course> map = new HashMap<>(); /** * 添加方法 */
    public void add(Course course){ map.put(course.getId(),course); } /** * 获取全部课程 */
    public Collection<Course> getAll(){ return map.values(); } }

Controller类中添加业务方法:数组

@Controller public class DataBindController { @Autowired private CourseDao courseDao; /** * 绑定POJO对象 */ @RequestMapping("/pojoType") public String pojoType(Course course, Model model){ //将课程添加到数据库
 courseDao.add(course); //封装数据
        model.addAttribute("courses",courseDao.getAll()); return "showData"; } }

建立addCourse.jsp页面spring-mvc

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加课程</title>
</head>
<body>
<form action="/pojoType" method="post">
    <p> 课程编号:<input type="text" name="id">
    </p>
    <p> 课程名称:<input type="text" name="name">
    </p>
    <p> 课程价格:<input type="text" name="price">
    </p>
    <p> 讲师姓名:<input type="text" name="author.name">
    </p>
    <p>
        <input type="submit" value="提交">
    </p>
</form>
</body>
</html>

 建立showData.jsp页面,显示数据:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>展现数据</title>
</head>
<body>

    <table border="1" width="50%">
        <tr>
            <th>课程编号</th>
            <th>课程名称</th>
            <th>课程价格</th>
            <th>讲师姓名</th>
        </tr>
        <c:forEach var="course" items="${courses}">
            <tr>
                <td>${course.id}</td>
                <td>${course.name}</td>
                <td>${course.price}</td>
                <td>${course.author.name}</td>
            </tr>
        </c:forEach>
    </table>

</body>
</html>

访问URL:

提交后结果:

五、绑定List集合

建立CourseList类,用于存储集合对象

public class CourseList { private List<Course> courses; public List<Course> getCourses() { return courses; } public void setCourses(List<Course> courses) { this.courses = courses; } }

Controller类中的业务方法:

/** * 绑定List集合 */ @RequestMapping("/listType") public String listType(CourseList courseList,Model model){ for (Course course : courseList.getCourses()){ courseDao.add(course); } model.addAttribute("courses",courseDao.getAll()); return "showData"; }

建立addCourseList.jsp页面,添加数据:

<form action="/listType" method="post">
    <p> 课程1编号:<input type="text" name="courses[0].id">
    </p>
    <p> 课程1名称:<input type="text" name="courses[0].name">
    </p>
    <p> 课程1价格:<input type="text" name="courses[0].price">
    </p>
    <p> 讲师姓名:<input type="text" name="courses[0].author.name">
    </p>
    <hr>
    <p> 课程2编号:<input type="text" name="courses[1].id">
    </p>
    <p> 课程2名称:<input type="text" name="courses[1].name">
    </p>
    <p> 课程2价格:<input type="text" name="courses[1].price">
    </p>
    <p> 讲师姓名:<input type="text" name="courses[1].author.name">
    </p>
    <p>
        <input type="submit" value="提交">
    </p>
</form>

访问URL:

提交后结果:

六、绑定Map集合

建立CourseMap类,用于存储Map数据:

public class CourseMap { private Map<String,Course> courses = new HashMap<>(); public Map<String, Course> getCourses() { return courses; } public void setCourses(Map<String, Course> courses) { this.courses = courses; } }

Controller类中的业务方法:

/** * 绑定Map集合 */ @RequestMapping("/mapType") public String mapType(CourseMap courseMap,Model model){ for (String key : courseMap.getCourses().keySet()){ Course course = courseMap.getCourses().get(key); courseDao.add(course); } model.addAttribute("courses",courseDao.getAll()); return "showData"; }

建立addCourseMap.jsp页面:

<form action="/mapType" method="post">
    <p> 课程1编号:<input type="text" name="courses['one'].id">
    </p>
    <p> 课程1名称:<input type="text" name="courses['one'].name">
    </p>
    <p> 课程1价格:<input type="text" name="courses['one'].price">
    </p>
    <p> 讲师姓名:<input type="text" name="courses['one'].author.name">
    </p>
    <hr>
    <p> 课程2编号:<input type="text" name="courses['two'].id">
    </p>
    <p> 课程2名称:<input type="text" name="courses['two'].name">
    </p>
    <p> 课程2价格:<input type="text" name="courses['two'].price">
    </p>
    <p> 讲师姓名:<input type="text" name="courses['two'].author.name">
    </p>
    <p>
        <input type="submit" value="提交">
    </p>
</form>

访问URL:

提交后结果:

七、绑定Set集合

建立CourseSet类,必需要在无参构造中,向set集合添加两个对象!

public class CourseSet { private Set<Course> courses = new HashSet<>(); public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } public CourseSet() { courses.add(new Course()); courses.add(new Course()); } }

 Controller类中的业务方法:

/** * 绑定Set集合 */ @RequestMapping("/setType") public String setType(CourseSet courseSet,Model model){ for (Course course : courseSet.getCourses()){ courseDao.add(course); } model.addAttribute("courses",courseDao.getAll()); return "showData"; }

建立addCourseSet.jsp页面

<form action="/setType" method="post">
    <p> 课程1编号:<input type="text" name="courses[0].id">
    </p>
    <p> 课程1名称:<input type="text" name="courses[0].name">
    </p>
    <p> 课程1价格:<input type="text" name="courses[0].price">
    </p>
    <p> 讲师姓名:<input type="text" name="courses[0].author.name">
    </p>
    <hr>
    <p> 课程2编号:<input type="text" name="courses[1].id">
    </p>
    <p> 课程2名称:<input type="text" name="courses[1].name">
    </p>
    <p> 课程2价格:<input type="text" name="courses[1].price">
    </p>
    <p> 讲师姓名:<input type="text" name="courses[1].author.name">
    </p>
    <p>
        <input type="submit" value="提交">
    </p>
</form>

访问URL:

提交后结果:

八、绑定JSON数据

在pom.xml配置文件中添加jackson依赖:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.8.3</version>
</dependency>

在springmvc.xml文件中配置消息转换器:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
    </mvc:message-converters>
</mvc:annotation-driven>

若是在配置消息转换器时,出现报红线,多是约束的缘由,只须要在springmvc.xml的约束中添加如下配置:

<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">

Controller类中的业务方法:

/** * 绑定JSON数据 */ @RequestMapping("/jsonType") @ResponseBody public Course jsonType(@RequestBody Course course){ course.setPrice(course.getPrice()+100); return course; }

建立sendJson.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>绑定JSON数据</title>
    <script 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>
</head>
<body>

</body>
</html>

发送URL:

相关文章
相关标签/搜索