一、RESTful的基本概念
REST(Representational State Transfer)表述性狀態轉移,REST並非一種創新技術,它指的是一組架構約束條件和原則,符合REST的約束條件和原則的架構,就稱它爲RESTful架構。
RESTful具體來說就是HTTP協議的四種形式表示四種基本操做:
GET(獲取資源)、POST(新建資源)、PUT(修改資源)、DELETE(刪除資源)html
二、RESTful架構的特色
統一了客戶端訪問資源的接口
url更加簡潔,易於理解,便於擴展
有利於不一樣系統之間的資源共享java
三、RESTful開發風格示例
- 查詢課程,method='get',http://localhost:8080/id
- 添加課程,method='post',http://localhost:8080/course
- 刪除課程,method='delete',http://localhost:8080/id
- 修改課程,method='put',http://localhost:8080/course
四、RESTful的代碼實現
4.一、配置過濾器
在web.xml配置文件中編輯代碼:web
<!--設置HTTP請求方式--> <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4.二、建立實體類
示例代碼:spring
public class Course { private int id; private String name; private double price; //getter,setter方法 }
4.三、建立DAO
示例代碼:數據庫
@Repository public class CourseDao { //模擬數據庫存儲 private Map<Integer, Course> courseMap = new HashMap<>(); /** * 添加課程 */ public void add(Course course){ courseMap.put(course.getId(),course); } /** * 查詢全部課程 */ public Collection<Course> getAll(){ return courseMap.values(); } /** * 經過ID查詢課程 */ public Course getById(int id){ return courseMap.get(id); } /** * 修改課程 */ public void update(Course course){ courseMap.put(course.getId(),course); } /** * 刪除課程 */ public void delete(int id){ courseMap.remove(id); } }
4.四、POST添加數據
Controller類中的業務方法:架構
@Controller public class CourseController { @Autowired private CourseDao courseDao; /** * 添加課程 */ @PostMapping("/add") public String add(Course course){ courseDao.add(course); System.out.println(course); return "redirect:/getAll"; } }
add.jsp頁面app
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %> <html> <head> <title>添加課程</title> </head> <body> <h1>添加課程</h1> <hr> <form action="${pageContext.request.contextPath}/add" 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="submit" value="提交"> </p> </form> </body> </html>
4.五、GET查詢數據
Controller類中的業務方法:jsp
/** * 查詢全部課程 */ @GetMapping("/getAll") public String getAll(Model model){ model.addAttribute("courses",courseDao.getAll()); System.out.println("getAll......."); return "index"; } /** * 根據ID查詢課程 */ @GetMapping("/getById/{id}") public String getById(@PathVariable("id") int id,Model model){ model.addAttribute("course",courseDao.getById(id)); return "edit"; }
index.jsp頁面,用於展現數據:post
<table border="1" width="80%"> <tr> <th>課程編號</th> <th>課程名稱</th> <th>課程價格</th> <th>編輯</th> </tr> <c:forEach items="${courses}" var="course"> <tr> <td>${course.id}</td> <td>${course.name}</td> <td>${course.price}</td> <td> <form action="${pageContext.request.contextPath}/getById/${course.id}" method="get"> <input type="submit" value="編輯"> </form> <form action="${pageContext.request.contextPath}/delete/${course.id}" method="post"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="刪除"> </form> </td> </tr> </c:forEach> </table>
4.六、PUT修改數據
Controller類中的業務方法:url
/** * 修改課程 */ @PutMapping("/update") public String update(Course course){ courseDao.update(course); return "redirect:/getAll"; }
edit.jsp頁面
<form action="${pageContext.request.contextPath}/update" method="post"> <p> 課程編號:<input type="text" name="id" value="${course.id}"> </p> <p> 課程名稱:<input type="text" name="name" value="${course.name}"> </p> <p> 課程價格:<input type="text" name="price" value="${course.price}"> </p> <p> <input type="hidden" name="_method" value="PUT"> <input type="submit" value="提交"> </p> </form>
4.七、DELETE刪除數據
Controller類中的業務方法:
/** * 刪除課程 */ @DeleteMapping("/delete/{id}") public String delete(@PathVariable("id") int id){ courseDao.delete(id); return "redirect:/getAll"; }
index.jsp頁面中的刪除按鈕:
<form action="${pageContext.request.contextPath}/delete/${course.id}" method="post"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="刪除"> </form>