SpringMVC学习笔记之---数据绑定
SpringMVC数据绑定
一.基础配置
(1)pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>
(2)web.xml
<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>
(3)springmvc.xml
<?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.xsd"> <!--配置包扫描--> <context:component-scan base-package="controller,dao"></context:component-scan> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--配置前缀--> <property name="prefix" value="/"></property> <!--配置后缀--> <property name="suffix" value=".jsp"></property> </bean> <!--消息转换器--> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
(4)Course.java(实体类)
package entity; public class Course { private int id; private String name; private double price; private Author author; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } 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; } @Override public String toString() { return "Course{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + ", author=" + author + '}'; } }
(5)Author.java(实体类)
package entity; public class Author { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
(6)SaveDao.java(用于保存的dao)
package dao; import entity.Course; import org.springframework.stereotype.Repository; import java.util.Collection; import java.util.HashMap; import java.util.Map; @Repository public class SaveDao { private Map<Integer,Course> map=new HashMap<Integer, Course>(); public void save(Course course){ map.put(course.getId(),course); } public Collection<Course> getall(){ return map.values(); } }
(7)index.jsp(用于遍历显示)
<%@ 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</title> </head> <body> <table> <tr> <td>课程ID</td> <td>课程名</td> <td>课程价格</td> <td>讲师ID</td> <td>讲师名</td> </tr> <c:forEach items="${courses}" var="course"> <tr> <td>${course.id}</td> <td>${course.name}</td> <td>${course.price}</td> <td>${course.author.id}</td> <td>${course.author.name}</td> </tr> </c:forEach> </table> </body> </html>
二.数据绑定分类
(一)普通数据类型的数据绑定
(二)包装类的数据绑定
(三)数组类型的数据绑定
(四)对象类型的数据绑定
(五)List集合类型的数据绑定
(六)Map集合类型的数据绑定
(七)Set集合类型的数据绑定
DataController.java
package controller; import dao.SaveDao; import entity.*; 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 DataController { @Autowired private SaveDao saveDao; /** * 基本数据类型的数据绑定 * @param id * @return */ @RequestMapping("/one") @ResponseBody //将返回值显示到页面 public String one(@RequestParam(value="id") int id){ return "id:"+id; } /** * 包装类的数据绑定 * @param id * @return */ @RequestMapping("/two") @ResponseBody public String two(@RequestParam(value="id")Integer id){ return "id"+id; } /** * 数组类型的数据绑定 * @param three * @return */ @RequestMapping("/three") @ResponseBody public String three(String[] three){ StringBuffer st=new StringBuffer(); for(String s:three){ st.append(s).append(" "); } return st.toString(); } /** * 对象类型的数据绑定 * 对象保存,在dao层进行 * @param course * @return */ @RequestMapping("/four") public ModelAndView four(Course course){ saveDao.save(course); ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("index"); modelAndView.addObject("courses",saveDao.getall()); return modelAndView; } /** * List集合类型的数据绑定 * 不能直接绑定集合,应该创建一个包装类 * @param courceList * @return */ @RequestMapping("/five") public ModelAndView five(CourceList courceList){ for(Course course:courceList.getCourses()){ saveDao.save(course); } ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("index"); modelAndView.addObject("courses",saveDao.getall()); return modelAndView; } /** * Map集合类型的数据绑定 * @param courseMap * @return */ @RequestMapping("/six") public ModelAndView six(CourseMap courseMap){ for(Course course:courseMap.getMap().values()){ System.out.println(course); saveDao.save(course); } ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("index"); modelAndView.addObject("courses",saveDao.getall()); return modelAndView; } /** * Set集合类型的数据绑定 * @param courseSet * @return */ @RequestMapping("/seven") public ModelAndView seven(CourseSet courseSet){ for(Course course:courseSet.getSet()){ saveDao.save(course); } ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("index"); modelAndView.addObject("courses",saveDao.getall()); return modelAndView; } @RequestMapping(value="/eight") @ResponseBody public String eight(@RequestBody User user){ user.setName("成功"); System.out.println("=========="); return "aa"; } }
三.数据绑定所需的类和页面
(一)普通数据类型
(二)包装类
(三)数组类型
(四)对象类型
1.save.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/four" method="post"> 课程ID:<input type="text" name="id"> 课程名:<input type="text" name="name"> 课程价格:<input type="text" name="12.5"> 讲师ID:<input type="text" name="author.id"> 讲师名:<input type="text" name="author.name"> <input type="submit"> </form> </body> </html>
(五)List集合
1.CourseList.java
package entity; import java.util.List; public class CourceList { private List<Course> courses; public List<Course> getCourses() { return courses; } public void setCourses(List<Course> courses) { this.courses = courses; } }
2.saveList.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/five" method="post"> 课程1ID:<input type="text" name="courses[0].id"> 课程1名:<input type="text" name="courses[0].name"> 课程1价格:<input type="text" name="courses[0].price"> 讲师1ID:<input type="text" name="courses[0].author.id"> 讲师1名:<input type="text" name="courses[0].author.name"> 课程2ID:<input type="text" name="courses[1].id"> 课程2名:<input type="text" name="courses[1].name"> 课程2价格:<input type="text" name="courses[1].price"> 讲师2ID:<input type="text" name="courses[1].author.id"> 讲师2名:<input type="text" name="courses[1].author.name"> <input type="submit"> </form> </body> </html>
(六)Map集合
1.CourseMap.java
package entity; import java.util.Map; public class CourseMap { private Map<String,Course> map; public Map<String, Course> getMap() { return map; } public void setMap(Map<String, Course> map) { this.map = map; } @Override public String toString() { return "CourseMap{" + "map=" + map + '}'; } }
2.saveMap.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/six" method="post"> 课程1ID:<input type="text" name="map['one'].id"> 课程1名:<input type="text" name="map['one'].name"> 课程1价格:<input type="text" name="map['one'].price"> 讲师1ID:<input type="text" name="map['one'].author.id"> 讲师1名:<input type="text" name="map['one'].author.name"> 课程2ID:<input type="text" name="map['two'].id"> 课程2名:<input type="text" name="map['two'].name"> 课程2价格:<input type="text" name="map['two'].price"> 讲师2ID:<input type="text" name="map['two'].author.id"> 讲师2名:<input type="text" name="map['two'].author.name"> <input type="submit"> </form> </body> </html>
(七)Set集合
1.CourseSet.java
package entity; import java.util.HashSet; import java.util.Set; public class CourseSet { private Set<Course> set=new HashSet<Course>(); public Set<Course> getSet() { return set; } public void setSet(Set<Course> set) { this.set = set; } public CourseSet(){ set.add(new Course()); set.add(new Course()); } }
2.saveSet.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/seven" method="post"> 课程1ID:<input type="text" name="set[0].id"> 课程1名:<input type="text" name="set[0].name"> 课程1价格:<input type="text" name="set[0].price"> 讲师1ID:<input type="text" name="set[0].author.id"> 讲师1名:<input type="text" name="set[0].author.name"> 课程2ID:<input type="text" name="set[1].id"> 课程2名:<input type="text" name="set[1].name"> 课程2价格:<input type="text" name="set[1].price"> 讲师2ID:<input type="text" name="set[1].author.id"> 讲师2名:<input type="text" name="set[1].author.name"> <input type="submit"> </form> </body> </html>
———————————–demo12————————————