一、 在HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。
它们分别对应四种基本操作:
1、GET ====== 获 取资源
2、POST ======新建资源
3、PUT======= 更新资源
4、DELETE==== 删除资源
二、REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便, 所以正得到越来越多网站的采用。
我们可以通过rest风格占位符方式,利用@PathVariable注解将占位符的值赋给调用方法参数,实现结果:
/某路径/1 HTTP GET : 得到 id = 1 的 一条数据
/某路径/1 HTTP DELETE: 删除 id = 1的 一条数据
/某路径/1 HTTP PUT: 更新id = 1的 一条数据
/某路径 HTTP POST: 新增一条数据
实现方式(REST风格四种请求方式的调用):
我们通过@RequestMapping映射请求中的method参数实现四种请求方式的调用,以下为示例代码。
GET请求:
1
2
3
4
5
6
7
8
|
@RequestMapping(value="/student",method=RequestMethod.GET) public ModelAndView toAddPage(){ ModelAndView mView=new ModelAndView(); mView.addObject("employee",new Employee()); mView.setViewName("add-stu"); mView.addObject("departments", departmentDao.getDepartments()); return mView; } |
POST请求:
1
2
3
4
5
|
@RequestMapping(value="/student",method=RequestMethod.POST) public String addStu(Employee employee){ employeeDao.save(employee); return "redirect:/show" ; } |
DELETE请求:
1
2
3
4
5
|
@RequestMapping(value="/student/{id}",method=RequestMethod.DELETE) public String deleteStu(@PathVariable(value="id") Integer id){ employeeDao.delete(id); return "redirect:/show" ; } |
PUT请求:
1
2
3
4
5
|
@RequestMapping(value="/student",method=RequestMethod.PUT) public String Update(@RequestParam(value="id")Integer id,Employee employee){ employeeDao.save(employee); return "redirect:/show" ; } |
三、将POST请求转化为put请求和delele请求
1.在web.xml文件中配置HiddenHttpMethodFilter过滤器:
1
2
3
4
5
6
7
8
9
|
<!-- HiddenHttpMethodFilter过滤器可以将POST请求转化为put请求和delete请求! --> < filter > < filter-name >hiddenHttpMethodFilter</ filter-name > < filter-class >org.springframework.web.filter.HiddenHttpMethodFilter</ filter-class > </ filter > < filter-mapping > < filter-name >hiddenHttpMethodFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > |
2.在表单域中需要携带一个name值为_method,value值为put或者delete的参数,如下所示:
1
2
3
|
< form action = "" method = "post" > < input type = "hidden" name = "_method" value = "delete" > </ form > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< form:form action = "${pageContext.request.contextPath}/student" method = "post" modelAttribute = "employee" > < c:if test = "${empty employee.id }" > 姓名:< form:input path = "lastName" />< br > </ c:if > < c:if test = "${!empty employee.id }" > 姓名:< form:input path = "lastName" readonly = "true" />< br > < form:hidden path = "id" /> < input type = "hidden" name = "_method" value = "put" > </ c:if > 邮箱:< form:input path = "email" />< br > <% Map< String ,Object>map=new HashMap< String ,Object>(); map.put("1","Male"); map.put("0", "Female"); request.setAttribute("genders", map); %> 性别:< form:radiobuttons path = "gender" items = "${genders}" />< br > 部门:< form:select path = "department.id" items = "${departments}" itemValue = "id" itemLabel = "departmentName" ></ form:select >< br > < input type = "submit" value = "提交" > </ form:form > |
最后在Controller层调用即可。根据@RequestMapping的value值以及携带的参数、请求方式查找匹配函数。
以上这篇SpringMVC的REST风格的四种请求方式总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/alternative/archive/2017/08/28/7445742.html