how2j.cn

相关下载
文件名 文件大小
springboot.rar 34k
使用站长秘制下载工具
步骤 1 : Restful 风格是什么   
步骤 2 : 先运行,看到效果,再学习   
步骤 3 : 模仿和排错   
步骤 4 : 基于前面的知识点   
步骤 5 : listCategory.jsp   
步骤 6 : editCategory.jsp   
步骤 7 : CategoryController   

步骤 1 :

Restful 风格是什么

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

/addCategory?name=xxx

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

/category

CRUD如下表所示,URL就都使用一样的 "/category",区别只是在于method不同,服务器根据method的不同来判断浏览器期望做的业务行为
"); window.frames["iframe_show7232"].document.write(decodeHtml($("textarea#stepcodeTextarea7232").val())); window.frames["iframe_show7232"].document.close(); $(window.frames["iframe_show7232"]).load(function(){ $("#iframe_show7232").height($("#iframe_show7232").contents().find("body").height()+showittryitheight); }); $("#iframe_show7232").height($("#iframe_show7232").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
<style> *{ font-size:18px; } table.t2{ border-collapse:collapse; } td{ text-align:left; } </style> <table class="t2" border="1" width="100%"> <tr> <td ></td> <td colspan="2" >传统风格</td> <td colspan="2">Restful风格</td> </tr> <tr> <td ></td> <td width="380px" >url</td> <td >method</td> <td >url</td> <td >method</td> </tr> <tr> <td >增加</td> <td >/addCategory?name=xxx</td> <td >POST</td> <td >/category</td> <td >PUT</td> </tr> <tr> <td >删除</td> <td >/deleteCategory?id=123</td> <td >GET</td> <td >/category/123</td> <td >DELETE</td> </tr> <tr> <td >修改</td> <td >/updateCategory?id=123&name=yyy</td> <td >POST</td> <td >/category/123</td> <td >POST</td> </tr> <tr> <td >获取</td> <td >/getCategory?id=123</td> <td >GET</td> <td >/category/123</td> <td >GET</td> </tr> <tr> <td >查询</td> <td >/listCategory</td> <td >GET</td> <td >/category</td> <td >GET</td> </tr> </table>
"); window.frames["iframe7232"].document.write(decodeHtml(code7232)); window.frames["iframe7232"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7232"]).load(function(){ $("#iframe7232").height($("#iframe7232").contents().find("body").height()+showittryitheight); }); $("#iframe7232").height($("#iframe7232").contents().find("body").height()+showittryitheight); alreadyWriteCode7232 = code7232; $("#rendering7232").hide(); $("#rendered7232").show(); } var tRereshRetry2DemoPanel7232 = setInterval(rereshRetry2DemoPanel7232,1000); var binded7232 = false; $("textarea#stepcodeTextarea7232").keyup(function(){ if(!binded7232){ $(window).bind('beforeunload',function(){ binded7232 = true; return "xxxx"; }); } var newCode = $(this).val() code7232 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7232!=newCode){ // window.frames["iframe7232"].document.write("
"); // window.frames["iframe7232"].document.write(decodeHtml($("textarea#stepcodeTextarea7232").val())); // window.frames["iframe7232"].document.close(); // $(window.frames["iframe7232"]).load(function(){ // $("#iframe7232").height($("#iframe7232").contents().find("body").height()+showittryitheight); // }); // code7232 = newCode; // } }); $(".tryButton7232").click(function(){ $("#tryDiv7232").show(); $("#stepcodeTextarea7232").focus(); $("#stepcodeTextarea7232").height(200); $("#iframe7232").height(0); window.frames["iframe7232"].document.write("
"); window.frames["iframe7232"].document.write(decodeHtml($("textarea#stepcodeTextarea7232").val())); window.frames["iframe7232"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7232"]).load(function(){ $("#iframe7232").height($("#iframe7232").contents().find("body").height()+showittryitheight); }); $("#iframe7232").height($("#iframe7232").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7232.focus(); editor7232.setSize(null, "250"); $("#rendering7232").hide(); $("#rendered7232").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor7232 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7232"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor7232.on("change",function(doc){ if(!binded7232){ $(window).bind('beforeunload',function(){ binded7232 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7232 = newCode; $("textarea#stepcodeTextarea7232").val(newCode); if(alreadyWriteCode7232!=code7232){ lastModifedTime7232 = new Date().getTime(); $("#rendering7232").show(); $("#rendered7232").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7232 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7232.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7232").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
步骤 2 :

先运行,看到效果,再学习

老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
访问测试地址

http://127.0.0.1:8080/category

注: 启动方式是Springboot特有的,直接运行类:com.how2java.springboot.Application 的主方法。
先运行,看到效果,再学习
步骤 3 :

模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 4 :

基于前面的知识点

接下来我们就把 基于springboot jpa的crud和分页,修改为Restful风格
步骤 5 :

listCategory.jsp

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

<input type="hidden" name="_method" value="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的引入

<script type="text/javascript" src="js/jquery.min.js"></script>
<%@ 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="category/${c.id}">编辑</a></td> <td><a class="delete" href="category/${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="category" method="post"> <input type="hidden" name="_method" value="PUT"> 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修改为了 category/id
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <div style="margin:0px auto; width:500px"> <form action="../category/${c.id}" method="post"> name: <input name="name" value="${c.name}"> <br> <button type="submit">提交</button> </form> </div>
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8" isELIgnored="false"%>

<div style="margin:0px auto; width:500px">

<form action="../category/${c.id}" method="post">
name: <input name="name" value="${c.name}"> <br>
<button type="submit">提交</button>

</form>
</div>
步骤 7 :

CategoryController

CRUD的RequestMapping都修改为了/category,以前用的注解叫做@RequestMapper,现在分别叫做 GetMapper, PutMapper, PostMapper 和 DeleteMapper 用于表示接受对应的Method
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; @Controller public 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<Category> 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"; } }


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 JAVA 框架-SpringBoot-Restful 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图