| 
 
 资源定位及资源操作的风格,不是协议,可以遵循,也可以不遵循  
REST 即 Representational State Transfer (资源)表现层状态转化;  
用URL定位资源,用HTTP描述操作,是目前最流行的一种互联网软件架构;它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用;使用POST, DELETE, PUT, GET 分别对应 CRUD;Spring3.0 开始支持 REST 风格的请求  
http://localhost:8080/get.action?id=10 ? ??查询 get  
http://localhost:8080/add.action ???????????新增 post  
http://localhost:8080/update.action ????????修改 post  
http://localhost:8080/delete.action?id=10 ???删除 post  
http://localhost:8080/goods/1 查询GET  
http://localhost:8080/goods ???新增POST  
http://localhost:8080/goods ???更新PUT  
http://localhost:8080/goods/1 删除DELETE  
- 使用@PathVariable接收Restful风格参数
  
- test.jsp
  
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
??<title>index</title>
</head>
<body>
??????<a href="${pageContext.request.contextPath}/test/1/chen.action">测试restful风格传参</a><br>
</body>
</html>  
 ?  
- Controller
  
@Controller
public class TestController {
????@RequestMapping("/test/{id}/{name}")
????public String show(@PathVariable Integer id,@PathVariable String name){
????????System.out.println(id);
????????System.out.println(name);
????????return "result" ;
????}
}  
   
运行结果  
   
默认情况下Form表单是不支持PUT请求和DELETE请求的;  
spring3.0添加了一个过滤器HiddenHttpMethodFilter,可以将post请求转换为PUT或DELETE请求  
- 在web中配置过滤器
  
<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.xml文件  
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
?????????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?????????xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
?????????version="4.0">
????<!-- 配置SpringMVC前端控制器 -->
????<servlet>
????????<servlet-name>springMVC</servlet-name>
????????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
????????<!-- 指定SpringMVC配置文件 -->
????????<!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
????????<init-param>
????????????<param-name>contextConfigLocation</param-name>
????????????<param-value>classpath:springmvc.xml</param-value>
????????</init-param>
????????<!-- 启动服务器,加载SpringMVC控制器 ?-->
????????<load-on-startup>1</load-on-startup>
????</servlet>
????<servlet-mapping>
????????<servlet-name>springMVC</servlet-name>
????????<!-- 设置所有以action结尾的请求进入SpringMVC -->
????????<url-pattern>*.action</url-pattern>
????</servlet-mapping>
????<!-- 解决post乱码问题 -->
????<filter>
????????<filter-name>encoding</filter-name>
????????<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
????????<!-- 设置编码参是UTF8 -->
????????<init-param>
????????????<param-name>encoding</param-name>
????????????<param-value>UTF-8</param-value>
????????</init-param>
????</filter>
????<filter-mapping>
????????<filter-name>encoding</filter-name>
????????<url-pattern>/*</url-pattern>
????</filter-mapping>
????<!--
????配置HiddenHttpMethodFilter过滤器
????实现restful请求
????-->
????<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>  
- 发送请求(test.jsp)
  
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
??<title>index</title>
</head>
<body>
?????<form action="${pageContext.request.contextPath}/test/18.action" method="post">
????????<%-- ??定义一个隐藏的表单
???????????????name值必须为_method
??????????????value为请求方式
????????--%>
?????????<input type="hidden" name="_method" value="put">
?????????<input type="submit" value="提交请求">
?????</form>
</body>
</html>  
?   
- 处理请求(Controller)
  
@Controller
public class TestController {
????@RequestMapping(value = "/test/{id}", method = RequestMethod.PUT)
????public String show(@PathVariable Integer id){
????????System.out.println(id);
????????return "result" ;
????}
}  
   
4、运行结果  
   
点击提交请求,成功访问方法并输出结果  
   
?  
如果我们把jsp里隐藏表单的value值该为delete,其他不变,此时访问则失败  
   
 ?  
因为在服务器处理方法里只配置了put方式的请求  
5、注意事项  
从tomcat8开始,如果直接返回jsp页面,会报405错误 ?JSPs only permit GET POST or HEAD;可以使用重定向的形式跳转到对应jsp或者是直接把对应jsp的 isErrorPage="true" 
                
        
    
 
 |