博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot之restful风格
阅读量:4583 次
发布时间:2019-06-09

本文共 4378 字,大约阅读时间需要 14 分钟。

步骤一:restful风格是什么?

  我们知道在做web开发的过程中,method常用的值是get和post。可事实上,method值还可以是put和delete等等其他值。

  既然method值如此丰富,那么就可以考虑使用同一个url,但是约定不同的method来实施不同的业务,这就是restful的基本考虑。

  CRUD是最常见的操作,在使用restful风格之前,通常的增加的方法是这样的:

/addCategory?name=xxx

  可以使用了restful风格之后,增加就变成了:

/category

  CRUD如下表所示,URL就都使用一样的 "/category",区别只是在于method不同,服务器根据method的不同来判断浏览器期望做的业务行为

步骤二:基于前面的知识点

  接下来我们就把基于springboot jpa的curd和分页,修改为Restful风格。

步骤三:修改listCategory.jsp

  listCategory.jsp 做了如下修改

  1. 增加
    1.1 action修改为"category"
    1.2 增加如下filed, 虽然这个form的method是post, 但是spingboot看到这个_method的值是put后,会把其修改为put.

   

  2. 删除

    2.1 url修改为category/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修改为了/category/id
  4. 在最开始增加了jquery.min.js的引入

  listCategory.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>         
id name 编辑 删除
${c.id} ${c.name} 编辑 删除
   添加分类: name:

步骤四:修改editCategory.jsp

  action修改为了 category/id

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> 
name:

步骤五:修改CategoryController

  CRUD的RequestMapping都修改为了/category,以前用的注解叫做@RequestMapper,现在分别叫做 GetMapper, PutMapper, PostMapper 和 DeleteMapper 用于表示接受对应的Method

package cn.xdf.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 cn.xdf.springboot.dao.CategoryDao;import cn.xdf.springboot.pojo.Category; @Controllerpublic class CategoryController {    @Autowired CategoryDao categoryDao;        @GetMapping("/category")     //查询所有    public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@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
page =categoryDao.findAll(pageable); m.addAttribute("page", page); return "listCategory"; } @PutMapping("/category") //增加 public String addCategory(Category c) throws Exception { categoryDao.save(c); return "redirect:/category"; } @DeleteMapping("/category/{id}") //删除 public String deleteCategory(Category c) throws Exception { categoryDao.delete(c); return "redirect:/category"; } @PostMapping("/category/{id}") //修改保存 public String updateCategory(Category c) throws Exception { categoryDao.save(c); return "redirect:/category"; } @GetMapping("/category/{id}") //修改查询 public String addCategory(@PathVariable("id") int id,Model m) throws Exception { Category c= categoryDao.getOne(id); m.addAttribute("c", c); return "editCategory"; }}

 

转载于:https://www.cnblogs.com/zs-notes/p/9390498.html

你可能感兴趣的文章
cocos2d-x游戏开发系列教程-超级玛丽01-前言
查看>>
如何做一个快乐的人
查看>>
外网无法访问tomcat
查看>>
[LeetCode] Delete Duplicate Emails
查看>>
excel如何用公式判断单元格的值是否为数字、英文、中文,以及相应的计数
查看>>
软件协作工具Trello
查看>>
快速搭建windows服务器的可视化运维环境
查看>>
java多线程读取、操作List集合
查看>>
Jboss EAP 6 EJB调用常见问题
查看>>
SQL优化 查询语句中,用 inner join 作为过滤条件和用where作为过滤条件的区别
查看>>
mongodb入门
查看>>
猫眼电影top100抓取
查看>>
【codeforces】【比赛题解】#862 CF Round #435 (Div.2)
查看>>
SpringCloud学习笔记(8)----Spring Cloud Netflix之负载均衡-Ribbon的负载均衡的策略
查看>>
并发编程学习笔记(3)----synchronized关键字以及单例模式与线程安全问题
查看>>
2-9
查看>>
python多线程(一)
查看>>
MindManager中读图工具的使用
查看>>
利用GridView 插入、删除、修改、分页的综合实例代码---转!!!
查看>>
2016年3月11日Android学习日记
查看>>