大家在做Web开发的过程中,method常用的值是get和post. 可事实上,method值还可以是put和delete等等其他值。
既然method值如此丰富,那么就可以考虑使用同一个url,但是约定不同的method来实施不同的业务,这就是Restful的基本考虑。
CRUD是最常见的操作,在使用Restful 风格之前,通常的增加做法是这样的:

/addCategory?name=xxx

可是使用了Restful风格之后,增加就变成了:

/categories

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

  传统风格 Restful风格
  url method url method
增加 /addCategory?name=xxx POST /categories POST
删除 /deleteCategory?id=123 GET /categories/123 DELETE
修改 /updateCategory?id=123&name=yyy POST /categories/123 PUT
获取 /getCategory?id=123 GET /categories/123 GET
查询 /listCategory GET /categories GET

下面是jsp中的实例:

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"%> <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>

editCategory.jsp

<%@ 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>

CategoryController中的写法:

package com.how2java.springboot.web;

import com.how2java.springboot.dao.CategoryDAO;
import com.how2java.springboot.pojo.Category;
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.*; import java.util.List; @Controller
public class CategoryController {
@Autowired CategoryDAO categoryDAO; @GetMapping("/categories")
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<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";
}
}

Restful 风格的更多相关文章

  1. 新建 ASP.NET Core Web API 项目 -- RESTFul 风格 Hello World!

    一.创建一个空项目 请查看 新建 .NET Core 项目 -- Hello World! 一节,新建一个项目:    二.添加引用并修改配置为 Web API (.NET Core 已将 MVC/W ...

  2. Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理

    Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理 今天在使用spring4.1.4,使用ResponseBody注解返回JSON格式的数据的时候遇到406错误. 解决 ...

  3. PHP实现RESTful风格的API实例(三)

    接前一篇PHP实现RESTful风格的API实例(二) .htaccess :重写URL,使URL以 /restful/class/1 形式访问文件 Options +FollowSymlinks R ...

  4. PHP实现RESTful风格的API实例(二)

    接前一篇PHP实现RESTful风格的API实例(一) Response.php :包含一个Request类,即输出类.根据接收到的Content-Type,将Request类返回的数组拼接成对应的格 ...

  5. PHP实现RESTful风格的API实例(一)

    最近看了一些关于RESTful的资料,自己动手也写了一个RESTful实例,以下是源码 目录详情: restful/ Request.php 数据操作类 Response.php 输出类 index. ...

  6. Jmeter在restful风格接口测试中的应用

    1.如何下载安装 官网下载,一个压缩包apache-jmeter-3.0.zip,解压即可,打开bin目录下jmeter.bat即可打开软件. 2.熟悉界面 3.实际案例 测试restful风格接口 ...

  7. 用cxf开发restful风格的WebService

    我们都知道cxf还可以开发restful风格的webService,下面是利用maven+spring4+cxf搭建webService服务端和客户端Demo 1.pom.xml <projec ...

  8. Restful风格API接口开发springMVC篇

    Restful风格的API是一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机 ...

  9. Rest(Restful)风格的Web API跟RPC风格的SOAP WebService--这些名词都啥意思?

    经常看到这些词汇,也有baidu或google过,但记忆里总是模糊,不确定,以至于别人问及的时候,总说不清楚.开篇随笔记录下.大家有补充或者意见的尽请留文. 本文顺序: 一.Rest(Restful) ...

  10. 通过beego快速创建一个Restful风格API项目及API文档自动化

    通过beego快速创建一个Restful风格API项目及API文档自动化 本文演示如何快速(一分钟内,不写一行代码)的根据数据库及表创建一个Restful风格的API项目,及提供便于在线测试API的界 ...

随机推荐

  1. Navicat连接腾讯云实例MySQL

    Navicat连接腾讯云实例MySQL 授权所有的用户通过root账户 root密码登陆远程数据库 连接腾讯云实例上的MySQL数据库 这里的密码填入数据库的密码 这里的密码填入登陆云实例的密码也就是 ...

  2. c++ 二叉树的遍历(迭代,递归)

    #include<iostream> #include <algorithm> #include <vector> #include <set> #in ...

  3. winform 导入 导出 excel

    https://blog.csdn.net/pp_fzp/article/details/51502233

  4. PAT Basic 1049 数列的片段和 (20 分)

    给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列 { 0.1, 0.2, 0.3, 0.4 },我们有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) ...

  5. java 中 IO 流分为几种?(未完成)

    java 中 IO 流分为几种?(未完成)

  6. Windows&Appium&Python自动化测试-Appium安装

    一.安装node.js 官方下载地址为:https://nodejs.org/en/download 傻瓜式安装即可,安装完成后,CMD中运行node -v查看版本号 输入npm 出现如上图信息,表示 ...

  7. golang docker kubernetes

    不断共建Golang生态.其中比较有代表性的Golang编写软件作品是Docker和Kubernetes.从目前Golang的发展时间和社区活跃度来看,Golang无疑是一门成功的编程语言.

  8. mysql_config_editor设置

    [root@node01 etc]# mysql_config_editor set -G mysql3307 -S /tmp/mysql3307.sock -uroot -pEnter passwo ...

  9. mongodb命令----批量更改文档字段名

    因为mongodb基于javascript的特性,为了体验cursor的威力我们不妨利用js的for循环创建记录 先创建文档 db.createCollection("columnsampl ...

  10. ACM-ICPC 2017 西安赛区现场赛 K. LOVER II && LibreOJ#6062. 「2017 山东一轮集训 Day2」Pair(线段树)

    题目链接:西安:https://nanti.jisuanke.com/t/20759   (计蒜客的数据应该有误,题目和 LOJ 的大同小异,题解以 LOJ 为准)     LOJ:https://l ...