SpringMVC 学习笔记(五) 基于RESTful的CRUD

来源:互联网 发布:天津市网络歌手大赛 编辑:程序博客网 时间:2024/06/03 13:04

1.1. 概述

当提交的表单带有_method字段时,通过HiddenHttpMethodFilter 将 POST 请求转换成 DELETEPUT请求,加上@PathVariable注解从而实现 RESTful 风格的CRUD


1.2. 配置信息

Web.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     version="3.0" >  
  6.     <!-- The front controller of this Spring Web application, responsible for handling all application requests -->  
  7.     <servlet>  
  8.         <servlet-name>springDispatcherServlet</servlet-name>  
  9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  10.         <init-param>  
  11.             <param-name>contextConfigLocation</param-name>  
  12.             <param-value>classpath:spring-mvc.xml</param-value>  
  13.         </init-param>  
  14.         <load-on-startup>1</load-on-startup>  
  15.     </servlet>  
  16.   
  17.     <!-- Map all requests to the DispatcherServlet for handling -->  
  18.     <servlet-mapping>  
  19.         <servlet-name>springDispatcherServlet</servlet-name>  
  20.         <url-pattern>/</url-pattern>  
  21.     </servlet-mapping>  
  22.       
  23.     <!--配置  HiddenHttpMethodFilter 可以将   POST 请求转为 DELETE、PUT 请求 -->  
  24.     <filter>  
  25.         <filter-name>hiddenHttpMethodFilter</filter-name>  
  26.         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
  27.     </filter>  
  28.       
  29.     <filter-mapping>  
  30.         <filter-name>hiddenHttpMethodFilter</filter-name>  
  31.         <url-pattern>/*</url-pattern>  
  32.     </filter-mapping>  
  33. </web-app>  

1.3. 效果

① 通过POST 请求增加员工信息

② 通过PUT 请求修改员工信息

③ 通过DELETE 请求删除员工信息

④ 通过GET 请求 获取所有的 员工信息




1.4. 代码

Employee.Java

[java] view plain copy
  1. package com.ibigsea.springmvc.model;  
  2.   
  3. public class Employee {  
  4.       
  5.     private Integer id;  
  6.     private String name;  
  7.     private String email;  
  8.     private int sex;  
  9.       
  10.     private Department department;  
  11.       
  12.     public Employee(Integer id, String name, String email, int sex,  
  13.             Department department) {  
  14.         super();  
  15.         this.id = id;  
  16.         this.name = name;  
  17.         this.email = email;  
  18.         this.sex = sex;  
  19.         this.department = department;  
  20.     }  
  21.       
  22.     public Employee() {  
  23.         super();  
  24.     }  
  25.   
  26.     public Integer getId() {  
  27.         return id;  
  28.     }  
  29.   
  30.     public void setId(Integer id) {  
  31.         this.id = id;  
  32.     }  
  33.   
  34.     public String getName() {  
  35.         return name;  
  36.     }  
  37.   
  38.     public void setName(String name) {  
  39.         this.name = name;  
  40.     }  
  41.   
  42.     public String getEmail() {  
  43.         return email;  
  44.     }  
  45.   
  46.     public void setEmail(String email) {  
  47.         this.email = email;  
  48.     }  
  49.   
  50.     public int getSex() {  
  51.         return sex;  
  52.     }  
  53.   
  54.     public void setSex(int sex) {  
  55.         this.sex = sex;  
  56.     }  
  57.   
  58.     public Department getDepartment() {  
  59.         return department;  
  60.     }  
  61.   
  62.     public void setDepartment(Department department) {  
  63.         this.department = department;  
  64.     }  
  65.   
  66.     @Override  
  67.     public String toString() {  
  68.         return "Employee [id=" + id + ", name=" + name + ", email=" + email  
  69.                 + ", sex=" + sex + ", department=" + department + "]";  
  70.     }  
  71.       
  72. }  

Department.java

[java] view plain copy
  1. package com.ibigsea.springmvc.model;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Department implements Serializable {  
  6.   
  7.     private static final long serialVersionUID = 6881984318733090395L;  
  8.       
  9.     private Integer id;  
  10.     private String name;  
  11.   
  12.     public Integer getId() {  
  13.         return id;  
  14.     }  
  15.     public void setId(Integer id) {  
  16.         this.id = id;  
  17.     }  
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.   
  25.     @Override  
  26.     public String toString() {  
  27.         return "Department [id=" + id + ", name=" + name + "]";  
  28.     }  
  29.       
  30. }  

RestfulController.java

[java] view plain copy
  1. package com.ibigsea.springmvc.rest;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.web.bind.annotation.ModelAttribute;  
  8. import org.springframework.web.bind.annotation.PathVariable;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10. import org.springframework.web.bind.annotation.RequestMethod;  
  11. import org.springframework.web.bind.annotation.RequestParam;  
  12.   
  13. import com.ibigsea.springmvc.dao.DepartmentDao;  
  14. import com.ibigsea.springmvc.dao.EmployeeDao;  
  15. import com.ibigsea.springmvc.model.Employee;  
  16.   
  17. /** 
  18.  * 基于Restful风格的增删改查 
  19.  * @author bigsea 
  20.  */  
  21. @Controller  
  22. @RequestMapping("/restful")  
  23. public class RestfulController {  
  24.       
  25.     @Autowired  
  26.     private EmployeeDao employeeDao;  
  27.       
  28.     @Autowired  
  29.     private DepartmentDao departmentDao;  
  30.       
  31.     /** 
  32.      * 因为修改的时候不能修改员工姓名, 
  33.      * 所以通过 @ModelAttribute 注解, 
  34.      * 表示在执行目标方法时,先获取该员工对象 
  35.      * 将员工对象存入 implicitMode 中 
  36.      * @param id 员工ID 
  37.      * @param map 实际传入的是implicitMode 
  38.      */  
  39.     @ModelAttribute  
  40.     public void getEmployee(@RequestParam(value="id",required=false) Integer id,Map<String,Object> map){  
  41.         if (id != null) {  
  42.             map.put("employee", employeeDao.getEmpById(id));  
  43.         }  
  44.     }  
  45.       
  46.     /** 
  47.      * 查看所有的员工信息 
  48.      * @param map 
  49.      * @return 
  50.      */  
  51.     @RequestMapping("/list")  
  52.     public String list(Map<String, Object> map){  
  53.         map.put("emps", employeeDao.getAll());  
  54.         return "list";  
  55.     }  
  56.       
  57.     /** 
  58.      * 跳转到员工添加页面 
  59.      * @param map 
  60.      * @return 
  61.      */  
  62.     @RequestMapping(value="/add",method=RequestMethod.GET)  
  63.     public String add(Map<String, Object> map){  
  64.         map.put("depts", departmentDao.getAll());  
  65.         map.put("action""add");  
  66.         return "emp";  
  67.     }  
  68.       
  69.     /** 
  70.      * 添加员工 
  71.      * @param emp 
  72.      * @return 
  73.      */  
  74.     @RequestMapping(value="/add",method=RequestMethod.POST)  
  75.     public String add(Employee emp){  
  76.         if (emp == null) {  
  77.             return "emp";  
  78.         }  
  79.         if (emp.getDepartment().getId() != null) {  
  80.             emp.setDepartment(departmentDao.getDepartmentById(emp.getDepartment().getId()));  
  81.         }  
  82.         employeeDao.save(emp);  
  83.         return "redirect:/restful/list";  
  84.     }  
  85.   
  86.     /** 
  87.      * 删除员工信息 
  88.      * @param id 
  89.      * @return 
  90.      */  
  91.     @RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE)  
  92.     public String delete(@PathVariable("id") Integer id){  
  93.         employeeDao.delEmpById(id);  
  94.         return "redirect:/restful/list";  
  95.     }  
  96.       
  97.     /** 
  98.      * 因为先执行了 @ModelAttribute 注解的方法, 
  99.      * 获取了该员工ID所对应的员工信息 
  100.      * 然后在将前台获取的员工数据存入获取的员工信息中, 
  101.      * 这样就不用提交name属性也可以获取到值 
  102.      * @param emp 
  103.      * @return 
  104.      */  
  105.     @RequestMapping(value="/edit",method=RequestMethod.PUT)  
  106.     public String edit(Employee emp){  
  107.         if (emp == null) {  
  108.             return "emp";  
  109.         }  
  110.         if (emp.getDepartment().getId() != null) {  
  111.             emp.setDepartment(departmentDao.getDepartmentById(emp.getDepartment().getId()));  
  112.         }  
  113.         employeeDao.save(emp);  
  114.         return "redirect:/restful/list";  
  115.     }  
  116.       
  117.     /** 
  118.      * 跳转到员工修改页面 
  119.      * @param id 员工ID 
  120.      * @param map implicitMode 
  121.      * @return  
  122.      */  
  123.     @RequestMapping(value="/edit/{id}",method=RequestMethod.GET)  
  124.     public String edit(@PathVariable("id") Integer id,Map<String, Object> map){  
  125.         map.put("emp", employeeDao.getEmpById(id));  
  126.         map.put("depts", departmentDao.getAll());  
  127.         map.put("action""edit");  
  128.         return "emp";  
  129.     }  
  130.       
  131.       
  132.       
  133. }  

EmployeeDao.java

[java] view plain copy
  1. package com.ibigsea.springmvc.dao;  
  2.   
  3. import java.util.Collection;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import org.springframework.stereotype.Component;  
  8.   
  9. import com.ibigsea.springmvc.model.Department;  
  10. import com.ibigsea.springmvc.model.Employee;  
  11.   
  12. @Component  
  13. public class EmployeeDao {  
  14.       
  15.     private static Map<Integer,Employee> emps = new HashMap<Integer, Employee>();  
  16.       
  17.     /** 
  18.      * 初始化员工信息 
  19.      */  
  20.     static {  
  21.         emps.put(1001new Employee(1001,"AA","AA@ibigsea.com",0,new Department(101"JAVA")));  
  22.         emps.put(1002new Employee(1002,"BB","BB@ibigsea.com",0,new Department(102".NET")));  
  23.         emps.put(1003new Employee(1003,"CC","CC@ibigsea.com",1,new Department(103"PHP")));  
  24.         emps.put(1004new Employee(1004,"DD","DD@ibigsea.com",0,new Department(104"C")));  
  25.     }  
  26.       
  27.     private static int employeeId = 1005;  
  28.       
  29.     /** 
  30.      * 保存员工信息 
  31.      * @param emp 
  32.      */  
  33.     public void save(Employee emp){  
  34.         if (emp.getId() == null) {  
  35.             emp.setId(employeeId++);  
  36.         }  
  37.         emps.put(emp.getId(), emp);  
  38.     }  
  39.       
  40.     /** 
  41.      * 获取所有的员工信息 
  42.      * @return 
  43.      */  
  44.     public Collection<Employee> getAll(){  
  45.         return emps.values();  
  46.     }  
  47.       
  48.     /** 
  49.      * 根据ID获取员工信息 
  50.      * @param id 
  51.      * @return 
  52.      */  
  53.     public Employee getEmpById(Integer id){  
  54.         return emps.get(id);  
  55.     }  
  56.       
  57.     /** 
  58.      * 根据ID删除员工信息 
  59.      * @param id 
  60.      */  
  61.     public void delEmpById(Integer id){  
  62.         emps.remove(id);  
  63.     }  
  64.       
  65. }  

DepartmentDao.java

[java] view plain copy
  1. package com.ibigsea.springmvc.dao;  
  2.   
  3. import java.util.Collection;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import org.springframework.stereotype.Component;  
  8.   
  9. import com.ibigsea.springmvc.model.Department;  
  10.   
  11. @Component  
  12. public class DepartmentDao {  
  13.   
  14.     public static Map<Integer, Department> depts = new HashMap<Integer, Department>();  
  15.       
  16.     /** 
  17.      * 初始化部门信息 
  18.      */  
  19.     static {  
  20.         depts.put(101new Department(101,"JAVA"));  
  21.         depts.put(102new Department(102,".NET"));  
  22.         depts.put(103new Department(103,"PHP"));  
  23.         depts.put(104new Department(104,"C"));  
  24.     }  
  25.       
  26.     /** 
  27.      * 获取所有的部门信息 
  28.      * @return 
  29.      */  
  30.     public Collection<Department> getAll(){  
  31.         return depts.values();  
  32.     }  
  33.       
  34.     /** 
  35.      * 根据ID获取部门信息 
  36.      * @param id 
  37.      * @return 
  38.      */  
  39.     public Department getDepartmentById(Integer id){  
  40.         return depts.get(id);  
  41.     }  
  42.       
  43. }  

List.jsp

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>员工信息</title>  
  9. </head>  
  10. <body>  
  11.   
  12.     <c:if test="${ empty emps }">  
  13.         没有员工信息  
  14.     </c:if>  
  15.     <c:if test="${ !empty emps }">  
  16.             <table border="1" bordercolor="black" cellspacing="0">  
  17.                 <tr>  
  18.                     <td width="40px">id</td>  
  19.                     <td width="30px">name</td>  
  20.                     <td width="80px">email</td>  
  21.                     <td width="30px">sex</td>  
  22.                     <td width="40px">编辑</td>  
  23.                     <td width="40px">删除</td>  
  24.                 </tr>  
  25.                 <c:forEach items="${emps }" var="emp">  
  26.                     <tr>  
  27.                         <td>${emp.id }</td>  
  28.                         <td>${emp.name }</td>  
  29.                         <td>${emp.email }</td>  
  30.                         <td>${emp.sex == 0 ? '男' : '女'}</td>  
  31.                         <td><a href="${pageContext.request.contextPath}/restful/edit/${emp.id }">Edit</a></td>  
  32.                         <td><form action="${pageContext.request.contextPath}/restful/delete/${emp.id }" method="post">  
  33.                                 <input type="hidden" name="_method" value="DELETE">  
  34.                                 <input type="submit" value="Delete">  
  35.                             </form>  
  36.                         </td>  
  37.                     </tr>  
  38.                 </c:forEach>  
  39.             </table>  
  40.     </c:if>  
  41.     <br><br>  
  42.     <a href="${pageContext.request.contextPath}/restful/add">添加员工</a>  
  43. </body>  
  44. </html>  

Emp.jsp

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>员工信息</title>  
  9. </head>  
  10. <body>  
  11.     <form action="${pageContext.request.contextPath}/restful/${action}" method="post">  
  12.         <c:if test="${!empty emp}">  
  13.             <input type="hidden" name="_method" value="PUT">  
  14.             <input type="hidden" name="id" value="${emp.id }"><br/><br/>  
  15.         </c:if>  
  16.         <c:if test="${empty emp}">name : <input type="text" name="name" value="${emp.name }"><br/><br/></c:if>  
  17.         email : <input type="text" name="email" value="${emp.email }"><br/><br/>  
  18.         sex :<input type="radio" name="sex" value="0" <c:if test="${emp.sex == 0}">checked</c:if>>男      
  19.         <input type="radio" name="sex" value="1" <c:if test="${emp.sex == 1}">checked</c:if>><br/><br/>  
  20.         department :<select name="department.id">  
  21.             <c:forEach items="${depts }" var="dept">  
  22.                 <option value="${dept.id }" <c:if test="${emp.department.id == dept.id}">selected</c:if>>${dept.name }</option>  
  23.             </c:forEach>  
  24.         </select>  
  25.         <br><br/>  
  26.         <input type="submit" value="提交">  
  27.     </form>  
  28. </body>  
  29. </html>  

1.5. 静态资源问题

因为配置了DispatcherServlet并且拦截了所有的请求,所以在添加静态资源的时候会访问不到静态资源,springMVC.xml配置

mvc:default-servlet-handler

将在 SpringMVC 上下文中定义一个DefaultServletHttpRequestHandler,它会对进入 DispatcherServlet 的请求进行筛查,如果发现是没有经过映射的请求,就将该请求交由 WEB应用服务器默认的 Servlet 处理,如果不是静态资源的请求,才由DispatcherServlet 继续处理

mvc:annotation-driven

会自动注册

RequestMappingHandlerMapping

RequestMappingHandlerAdapter 

ExceptionHandlerExceptionResolver 三个bean

就能访问到静态资源了

1.6. mvc:annotation-drivenmvc:default-servlet-handler

在配置SpringMVC配置文件时添加mvc:annotation-driven

<mvc:annotation-driven /> 会自动注册三个Bean

² RequestMappingHandlerMapping

² RequestMappingHandlerAdapter 

² ExceptionHandlerExceptionResolver 

还将提供以下支持:

– 支持使用 ConversionService 实例对表单参数进行类型转换

– 支持使用 @NumberFormat @DateTimeFormat注解完成数据类型的格式化

– 支持使用 @Valid 注解对 JavaBean 实例进行 JSR 303 验证

– 支持使用 @RequestBody 和 @ResponseBody 注解






0 0
原创粉丝点击