REST风格请求一目了然,有利于数据传输稳定,主要有四个动词组成:GET、POST、PUT、DELETE,这样便于团队开发,注:一般请求默认为GET请求。
示例代码如下:
index,jsp
<a href="${pageContext.request.contextPath}/testGET/1">testREST</a>
<form action="${pageContext.request.contextPath}/testPost/2" method="post">
<input type="submit" value="testpost请求"/>
</form>
<form action="${pageContext.request.contextPath}/testPut/3" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="testPUT请求">
</form>
<form action="${pageContext.request.contextPath}/testDELETE/4" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="testDELETE请求">
</form>
</body>
</html>
TestREST.java
@RequestMapping(value = "testREST/{id}",method = RequestMethod.GET)
public String testREST_GET(@PathVariable("id") Integer id){
System.out.println("get请求得到的id是:"+id);
return "get";
}
@RequestMapping(value = "testPost/{id}",method = RequestMethod.POST)
public String testREST_POST(@PathVariable("id") String id){
System.out.println("post请求得到的id:"+id);
return "post";
}
/**
* 出现405错误:JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS
* 三种简单处理的办法!
* 1.tomcat换到7.0以及以下版本
* 2.请求先转给一个Controller,再返回jsp页面
* 3.在你的success页面头部文件将
* <%@ page language="java" contentType="text/html; charset=UTF-8"
* pageEncoding="UTF-8" isErrorPage="true"%>
*
* ---------------------
* 作者:全场梦游c
* 来源:CSDN
* 原文:https://blog.csdn.net/qq_39404258/article/details/81980898
* 版权声明:本文为博主原创文章,转载请附上博文链接!
*注:我使用第三个方法,其他两个方法并未验证
*/
@RequestMapping(value = "testPut/{id}",method = RequestMethod.PUT)
public String testREST_PUT(@PathVariable("id") Integer id){
System.out.println("PUT请求获取的id:"+id);
return "put";
}
@RequestMapping(value = "testDELETE/{id}",method = RequestMethod.DELETE)
public String testREST_DELETE(@PathVariable("id") Integer id){
System.out.println("DELETE请求的id:"+id);
return "delete";
}