1.web.xml中添加filter过滤器 HiddenHttpMethodFilter 这个作用在于将rest类型的请求转化为标准的http类型 put delete 方法
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- org.springframework.web.filter.HiddenHttpMethodFilter 可以将put delete 转化为http可以识别的请求 -->
<!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http 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>
<servlet>
<servlet-name>springmvc-1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 以下是默认配置的DispatchServlet WEB-INF/[servlet-name]-servlet.xml -->
<!-- 以下是自己配置的 -->
<!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value>
</init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- rest的get和post跟http的写法一样。
put 和 delete 的请求需要在form表单中添加一个 name=”_method” value=”put/delete” 的隐藏域
Rest.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
================get=========================</br>
<form action="testRest/testRestGet/123" method="get">
姓名:<input name="username" type="text"></br> 密码 :<input
name="password" type="password"></br> 性别 :男<input name="sex"
type="radio" value="男"> 女<input name="sex" type="radio"
value="女"></br> <input type="reset" name="reset" value="清空">
<input type="submit" name="submit" value="rest_get">
</form>
================post=========================</br>
<form action="testRest/testRestPost" method="POST">
姓名:<input name="username" type="text"></br> 密码 :<input
name="password" type="password"></br> 性别 :男<input name="sex"
type="radio" value="男"> 女<input name="sex" type="radio"
value="女"></br> <input type="reset" name="reset" value="清空">
<input type="submit" name="submit" value="rest_post">
</form>
================put=========================</br>
<form action="testRest/testRestPut/232" method="post">
<input type="hidden" name="_method" value="put">
姓名:<input name="username" type="text"></br> 密码 :<input
name="password" type="password"></br> 性别 :男<input name="sex"
type="radio" value="男"> 女<input name="sex" type="radio"
value="女"></br> <input type="reset" name="reset" value="清空">
<input type="submit" name="submit" value="rest_put">
</form>
================delete=========================</br>
<form action="testRest/testRestDelete/232" method="post">
<input type="hidden" name="_method" value="delete">
姓名:<input name="username" type="text"></br> 密码 :<input
name="password" type="password"></br> 性别 :男<input name="sex"
type="radio" value="男"> 女<input name="sex" type="radio"
value="女"></br> <input type="reset" name="reset" value="清空">
<input type="submit" name="submit" value="rest_delete">
</form>
</body>
</html>
springmvc-1-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置自定义的扫描包 -->
<context:component-scan base-package="com/ztc/controller"></context:component-scan>
<!-- 配置视图解析器: 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 在controller中使用@PathVariable 注解
分别为
TestRest.java
package com.ztc.controller;
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;
/**
*
* @author jack
* 新增 /order post
* 删除/order/1 delete delete?id=1
* 修改/order/1 put update?id=1
* 查找/order get update?id=1
* 如何发送rest put delete请求?
* 1.需要配置 HiddenHttpMethodFilter
* 2. 需要发送POST 请求
* 3.需要发送POST请求时携带一个 name="_method" value="put/delete" 的隐藏域
* 在SpringMVC 的目标方法中如何得到id 呢?
* 使用@PathVariable 注解
*
*/
@Controller
@RequestMapping("/testRest")
public class TestRest {
private static final String SUCCESS ="success";
/**
* GET 查询
* @param id
* @return
*/
@RequestMapping("/testRestGet/{id}")
public String testRestGet(@PathVariable("id") Integer id){
System.out.println("测试REST风格的GET请求 "+id);
return SUCCESS;
}
/**
* Delete 删除
* @param id
* @return
*/
@RequestMapping(value="/testRestDelete/{id}",method=RequestMethod.DELETE)
public String testRestDelete(@PathVariable("id") Integer id){
System.out.println("测试REST风格的Delete请求 "+id);
return SUCCESS;
}
/**
* PUT 修改
* @param id
* @return
*/
@RequestMapping(value="/testRestPut/{id}",method=RequestMethod.PUT)
public String testRestPut(@PathVariable("id") Integer id){
System.out.println("测试REST风格的Put请求 "+id);
return SUCCESS;
}
/**
* POST 新增
* @param id
* @return
*/
@RequestMapping(value = "/testRestPost",method=RequestMethod.POST)
public String testRestPost(){
System.out.println("测试REST风格的POST请求 ");
return SUCCESS;
}
}
MyEclipse 控制台打印信息
测试REST风格的GET请求 123
测试REST风格的POST请求
测试REST风格的Put请求 232
测试REST风格的Delete请求 232