SpringMVC的REST风格的四种请求方式

一、 在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参数实现四种请求方式的调用:

@Controller
@RequestMapping("/login")
public class LoginAction {

	//http://localhost:8080/springmvcdemo/login/
	@RequestMapping(value="/")
	public String login() {
		return "login";
	}
	
	@RequestMapping(value="/restGet", method=RequestMethod.GET)
	public String restGet() {
		System.out.println("restGet");
		return "success";
	}
	@RequestMapping(value="/restPost", method=RequestMethod.POST)
	public String restPost() {
		System.out.println("restPost");
		return "success";
	}
	@RequestMapping(value="/restPut", method=RequestMethod.PUT)
	public String restPut() {
		System.out.println("restPut");
		return "redirect:/login/";
	}
	@RequestMapping(value="/restDelete", method=RequestMethod.DELETE)
	public String restDelete() {
		System.out.println("restDelete");
		return "redirect:/login/";
	}
	
}

----
restPut
restDelete
restGet
restPost

三、将POST请求转化为put请求和delele请求

1.在 web.xml 文件中配置 HiddenHttpMethodFilter 过滤器:

	<!-- hiddenHttpMethodFilter过滤器:将指定的POST请求转化为put请求或delele请求 -->
	<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.在表单域中需写一个隐藏表单:<input type="hidden" name="_method" value="delete"> 来访问 REST的put或者delete请求,正常get/post请求可写可不写。name 值为 _method ,value 值为put或者delete

        <h3>登录页面</h3>
	<%-- <form action="<%=path %>/login/restGet" method="get" > --%>
	<%-- <form action="<%=path %>/login/restPost" method="post" > --%>
	<%-- <form action="<%=path %>/login/restPut" method="post" > --%>
	<form action="<%=path %>/login/restDelete" method="post" >
		<input type="hidden" name="_method" value="delete">
		<input type="text" name="username" />	<br/>
		<input type="password" name="password" /> <br/>
		<input type="submit" value="提交" /> 
		<input type="reset" value="重置" /><br/>
	</form>

使用springmvc 的from 标签: <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

jsp页面可省略那个隐藏表单, 但是查看页面源码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>   
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改用户</title>
</head>
<body>

	<h3>修改用户信息</h3>
	<form:form action="${pageContext.request.contextPath }/edit" method="put" modelAttribute="user">
		<!-- path属性:相当于普通form标签元素标签上的id和name的综合体,见页面源码  -->
		<!-- <input type="hidden" name="_moehod" value="put"/> 省略不写,会自动添加,见页面源码 -->
		用户编号:<form:hidden path="id"/> <br/>
		用户名:<form:input path="username"/>  <br/>
		用户密码:<form:input path="password"/>  <br/>
		email:<form:input path="email"/>  <br/>
		用户所属部门:<form:select path="department.id" itemValue="id" itemLabel="departmentname" items="${departments }"/>  <br/>
		<input type="submit" value="提交"> <br/>
	</form:form>
	
</body>
</html>

   

猜你喜欢

转载自blog.csdn.net/qq_42402854/article/details/82765583