17.SPRINGBOOT 如何开发 RESTFUL 风格的项目
listCategory.jsp
listCategory.jsp 做了如下修改
1. 增加
1.1 action修改为"categories"
1. 增加
1.1 action修改为"categories"
2. 删除
2.1 url修改为categories/id
2.2 点击超链后,会使用form提交,并且提交_method的值为delete,以达到和增加类似的效果
$(function(){
$(".delete").click(function(){
var href=$(this).attr("href");
$("#formdelete").attr("action",href).submit();
return false;
})
})
3. 获取
3.1 url修改为了/categories/id
4. 在最开始增加了jquery.min.js的引入
<script type="text/javascript" src="js/jquery.min.js"></script>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript"> /*将post method 改变为delete*/ $(function(){ $(".delete").click(function(){ var href=$(this).attr("href"); $("#formdelete").attr("action",href).submit(); return false; }) }) </script> <div align="center"></div><div style="width:500px;margin:20px auto;text-align: center"> <table align='center' border='1' cellspacing='0'> <tr> <td>id</td> <td>name</td> <td>编辑</td> <td>删除</td> </tr> <c:forEach items="${page.content}" var="c" varStatus="st"> <tr> <td>${c.id}</td> <td>${c.name}</td> <td><a href="categories/${c.id}">编辑</a></td> <td><a class="delete" href="categories/${c.id}">删除</a></td> </tr> </c:forEach> </table> <br> <div> <a href="?start=0">[首 页]</a> <a href="?start=${page.number-1}">[上一页]</a> <a href="?start=${page.number+1}">[下一页]</a> <a href="?start=${page.totalPages-1}">[末 页]</a> </div> <br> <form action="categories" method="post"> name: <input name="name"> <br> <button type="submit">提交</button> </form> <form id="formdelete" action="" method="POST" > <input type="hidden" name="_method" value="DELETE"> </form></div> |
步骤 6 :
editCategory.jsp
action修改为了 categories/id
注意:form 下增加 filed, 虽然这个form的method是post, 但是springmvc看到这个_method的值是put后,会把其修改为put.
注意:form 下增加 filed, 虽然这个form的method是post, 但是springmvc看到这个_method的值是put后,会把其修改为put.
<input type="hidden" name="_method" value="PUT">
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%><div style="margin:0px auto; width:500px"><form action="../categories/${c.id}" method="post"> <input type="hidden" name="_method" value="PUT">name: <input name="name" value="${c.name}"> <br><button type="submit">提交</button></form></div> |
步骤 7 :
CategoryController
CRUD的RequestMapping都修改为了/categories,以前用的注解叫做@RequestMapper,现在分别叫做 GetMapper, PutMapper, PostMapper 和 DeleteMapper 用于表示接受对应的Method
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package com.how2java.springboot.web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Pageable;import org.springframework.data.domain.Sort;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.RequestParam;import com.how2java.springboot.dao.CategoryDAO;import com.how2java.springboot.pojo.Category; @Controllerpublic class CategoryController { @Autowired CategoryDAO categoryDAO; @GetMapping("/categories") public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") intstart,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception { start = start<0?0:start; Sort sort = new Sort(Sort.Direction.DESC, "id"); Pageable pageable = new PageRequest(start, size, sort); Page<Category> page =categoryDAO.findAll(pageable); m.addAttribute("page", page); return "listCategory"; } @PostMapping("/categories") public String addCategory(Category c) throws Exception { categoryDAO.save(c); return "redirect:/categories"; } @DeleteMapping("/categories/{id}") public String deleteCategory(Category c) throws Exception { categoryDAO.delete(c); return "redirect:/categories"; } @PutMapping("/categories/{id}") public String updateCategory(Category c) throws Exception { categoryDAO.save(c); return "redirect:/categories"; } @GetMapping("/categories/{id}") public String getCategory(@PathVariable("id") int id,Model m) throws Exception { Category c= categoryDAO.getOne(id); m.addAttribute("c", c); return "editCategory"; }} |
浙公网安备 33010602011771号