<!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> <button class="btn btn-danger btn-sm delete_btn"> <span class="glyphicon glyphicon-trash">删除</span> </button> </td> </tr> </c:forEach>