2018年1月28日
功能:实现REST风格的URL(http://localhost/springmvc5/user/view/4),提供查看该ID用户信息明细的的功能;
【0】rest的概念
REST即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。
目前在三种主流的Web服务实现方案中,因为REST模式的Web服务与复杂的SOAP和XML-RPC对比来讲明显的更加简洁,越来越多的web服务开始采用REST风格设计和实现。例如,Amazon.com提供接近REST风格的Web服务进行图书查找;雅虎提供的Web服务也是REST风格的。
以下是网友的看法,在此做个引用:
REST本身不是架构,只是一种架构风格,理解它的时候要参考这个架构风格出现的环境所施加的约束条件。
REST的目的是“建立十年内不会过时的软件系统架构",所以它具备三个特点:
1. 状态无关 —— 确保系统的横向拓展能力
2. 超文本驱动,Fielding的原话是”hypertext-driven" —— 确保系统的演化能力
3. 对 resource 相关的模型建立统一的原语,例如:uri、http的method定义等 —— 确保系统能够接纳多样而又标准的客户端
从另外一个角度看,第一条保证服务端演化,第三条保证客户端演化,第二条保证应用本身的演化,这实在是一个极具抽象能力的方案。
下面通过一个功能实现来理解get请求的写法:
【1】先是前台View层
在sys.jsp加入添加代码:
<h4>------------------请在下面的填写“用户ID”进行明细查询:---------</h4>
<br>
<a href="${pageContext.request.contextPath }/user/view/4" type="button">ID_Check</a>
<a href="${pageContext.request.contextPath }/user/view/${currentUser.id}" type="button">ID_Check_currentUser</a>
<br>
页面效果:
图1
【2】Controller层写处理方法
在UserController.java添加代码:
@RequestMapping(value="/view/{id}",method=RequestMethod.GET)
public String viewUser(@PathVariable String id,Model model) throws ParseException, SQLException{
System.out.println("view id ======="+id);
User user1 = userService.getUserById(id);
model.addAttribute("user1",user1);
System.out.println("user1: "+user1.toString());
return "userview";
}
【3】回到前台,处理结果显示
在userview.jsp添加代码:
<h5>userview页面</h5>
user1:${user1.id},${user1.userName},${user1.userCode},${user1.userPassword},${user1.birthday},
,${user1.phone},${user1.address}
【4】输出结果:
图2
对比数据库记录:
图3
【5】补充与拓展
1.url写法:
get:/restUrl/{id}; post:/restUrl ; delete:/restUrl/{id} ; put:/restUrl2.controller写法:
1)GET请求的目标方法:
@RequestMapping(value="/restUrl/{id}", method=RequestMethod.GET) public String get(Map<String, Object> map, @PathVariable("id") Integer id){ Object obj = new Object(); map.put("obj", obj); return "success"; }
注意:
1.必须在@RequestMapping注解中添加method=RequestMethod.GET,表明这是一个处理get请求的目标方法
2.通过@PathVariable("id") Integer id注解,将url中的{id}值取出,并赋值给该注解修饰的入参id
2)POST请求的目标方法:
@RequestMapping(value="/restUrl", method=RequestMethod.POST) public String post(Object obj){ System.out.println(obj); return "success"; }
注意:
1.必须在@RequestMapping注解中添加method=RequestMethod.POST,表明这是一个处理post请求的目标方法
2.post请求的url中不需要写参数{id}
3)DELETE请求的目标方法:
@RequestMapping(value="/restUrl/{id}", method=RequestMethod.DELETE) public String delete(@PathVariable("id") Integerid){ System.out.println(id); return "success"; }
注意:
1.必须在@RequestMapping注解中添加method=RequestMethod.DELETE,表明这是一个处理delete请求的目标方法
2.url中必须带有参数{id}
4)PUT请求的目标方法:
@RequestMapping(value="/restUrl", method=RequestMethod.PUT) public String put(Object obj){ System.out.println(obj); return "success"; }
注意:
1.必须在@RequestMapping注解中添加method=RequestMethod.PUT,表明这是一个处理put请求的目标方法
2.url中不需要带有参数{id}
3.如果需要使用@ModelAttribute来进行一些修改前的操作(如:先去数据库查询一个实体,在使用put目标方法),请参考@ModelAttribute注解的使用;
3.jsp页面中的链接写法:
1)get请求:
<a href="${pageContext.request.contextPath}/user/restUrl/{id}">get user</a>
注意:
1.这里的{id}不能直接写{id},而是你要动态赋值的
2)post请求:
<form action="${pageContext.request.contextPath }/restUrl" method="post" > name:<input type="text" name="username"><br> password:<input type="password" name="password"><br> <input type="submit" value="submit"> </form>
注意:
1.因为超链接是get请求,所以要使用post风格的url请求必须使用表单
2.必须表明表单的提交方式为method=post
3)delete请求:
<a class="delete_href" href="${pageContext.request.contextPath }/restUrl/{id}">remove</a>
<form id="delete_form" action="" method="post"> <input type="hidden" name="_method" value="DELETE"> </form>
$(function(){ $(".delete_href").on("click", function(){var href = $(this).attr("href"); $("#delete_form").attr("action", href).submit(); return false; }) })
注意:
1.由于超链接只能发送get请求,我们需要发送delete请求的话,必须通过一个表单提交,将表单的post请求,转换成delete请求
2.在表单中添加一个隐藏域<input type="hidden" name="_method" value="DELETE">,能让表单在提交的时候将请求转换成delete请求
3.用js实现在点击超链接时,实际上提交的是表单。但是要注意在js之前,请先引入jquery文件
4)put请求:
<form action="${pageContext.request.contextPath }/restUrl" method="post" > <input type="hidden" name="_method" value="PUT"> <input type="hidden" name="id" value="${id }"> name:<input type="text" name="username"><br> password:<input type="password" name="password"><br> <input type="submit" value="submit"> </form>
注意:
1.跟delete请求类似,我们需要一个隐藏域<input type="hidden" name="_method" value="PUT">,来将post请求转换成put请求
参考博文:https://www.cnblogs.com/javafucker/p/7509999.html;