REST风格
直接通过请求方法来对应CRUD操作
新增:/order POST
修改: /order/1 PUT 指定要修改的id
获取:/order/1 GET
删除: /order/1 DELETE
REST实现
在REST风格中,GET用来获取资源,POST用来新建资源,PUT用来更新或新建资源,DELETE用来删除资源。Spring中是通过HiddenHttpMethodFilter过滤器来将表单中传统的POST方式转换为PUT、DELETE方式。
注意在Tomcat8中,当请求为PUT或者DELETE时,会提示JSPs only permit GET POST or HEAD错误。应该是Tomcat8的bug。据说在版本7中没有此问题。
具体实现方法:
1.在web.xml配置过HiddenHttpMethodFilter滤器。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>spring</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param><!-- 配置SpringMVC的配置文件位置 -->
<!-- 这里可以不配置xml,使用默认位置WEB-INF/<servlet-name>-servlet.xml -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
</web-app>
2.在定义表单时添加如下隐藏项目,原始的POST和GET则不需要添加,默认即可。
<input type="hidden" name="_method" value="DELETE"><input type="hidden" name="_method" value="PUT">
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="GET" action="/spring/testREST/1">
<input type="submit" value="GET">
</form>
<form method="post" action="/spring/testREST">
<input type="submit" value="POST">
</form>
<form method="post" action="/spring/testREST/3">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="DELETE">
</form>
<form method="post" action="/spring/testREST/4">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="DELETE">
</form>
</body>
</html>
3.在控制器中配置请求映射。
如下例子所示:如果想取得URL中的数据作为参数,可以通过路径占位符取得 PathVariable 注解。
@PathVariable注解可以将URL中的占位符映射到ReqeustMapping注解的的方法中。
如下的方法中,会将URL中对应id的部分作为参数传递到函数中。
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class TestREST {
private static final String RESTVIEW="rest_view";
@RequestMapping(value="/testREST/{id}", method=RequestMethod.GET)
public String testGet(@PathVariable("id") Integer id){
System.out.println(id);
return RESTVIEW;
}
@RequestMapping(value="/testREST", method=RequestMethod.POST)
public String testPost(){
System.out.println("POST");
return RESTVIEW;
}
@RequestMapping(value="/testREST/{id}", method=RequestMethod.PUT)
public String testPut(@PathVariable("id") Integer id){
System.out.println(id);
return RESTVIEW;
}
@RequestMapping(value="/testREST/{id}", method=RequestMethod.DELETE)
public String testDelete(@PathVariable("id") Integer id){
System.out.println(id);
return RESTVIEW;
}
}
<完>