<!-- 4、使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 -->

详细使用请参考这篇博客:地址:http://blog.csdn.net/pplcheer/article/details/74999748

Rest 风格的 URL.
以 CRUD 为例:
新增: /order POST
修改: /order/1 PUT update?id=1
获取:/order/1 GET get?id=1
删除: /order/1 DELETE delete?id=1

如何发送 PUT 请求和 DELETE 请求呢 ?
 1. 需要在web.xml文件中配置 HiddenHttpMethodFilter 
<!--
配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 POST 请求
-->
<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> <filter>
<filter-name>HttpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpPutFormContentFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
补充说明:
如果在web.xml中配置HttpPutFormContentFilter,配置如下
<filter>
<filter-name>httpPutFormcontentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpPutFormContentFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

需要注意的是,该过滤器只能接受enctype值为application/x-www-form-urlencoded的表单,也就是说,在使用该过滤器时,form表单的代码必须如下:

<form action="" method="put" enctype="application/x-www-form-urlencoded">
......
</form>
2. 需要发送 POST 请求
3. 需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT
jsp文件如下:
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="TestRest PUT"/>
</form>
<br><br> <form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="TestRest DELETE"/>
</form>
<br><br> <form action="springmvc/testRest" method="post">
<input type="submit" value="TestRest POST"/>
</form>
<br><br> <a href="springmvc/testRest/1">Test Rest Get</a>
<br><br> 在 SpringMVC 的目标方法中如何得到 id 呢? 使用 @PathVariable 注解 */
@RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
public String testRestPut(@PathVariable Integer id) {
System.out.println("testRest Put: " + id);
return SUCCESS;
} @RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
public String testRestDelete(@PathVariable Integer id) {
System.out.println("testRest Delete: " + id);
return SUCCESS;
} @RequestMapping(value = "/testRest", method = RequestMethod.POST)
public String testRest() {
System.out.println("testRest POST");
return SUCCESS;
} @RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET)
public String testRest(@PathVariable Integer id) {
System.out.println("testRest GET: " + id);
return SUCCESS;
} /**
* @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中.
* @param id
* @return
*/
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id) {
System.out.println("testPathVariable: " + id);
return SUCCESS;
} @RequestMapping("/testAntPath/*/abc")
public String testAntPath() {
System.out.println("testAntPath");
return SUCCESS;
}

Spring MVC 使用拦截器 HiddenHttpMethodFilter配置Rest风格的URL的更多相关文章

  1. Spring mvc登录拦截器

    自己实现的第一个Spring mvc登录拦截器 题目要求:拒绝未登录用户进入系统,只要发现用户未登录,则将用户请求转发到/login.do要求用户登录 实现步骤: 1.在spring的配置文件中添加登 ...

  2. 玩转spring MVC(七)----拦截器

    继续在前边的基础上来学习spring MVC中拦截器的使用,下面通过一个例子来实现(完整项目在这里下载:http://download.csdn.net/detail/u012116457/84334 ...

  3. spring mvc +cookie+拦截器功能 实现系统自动登陆

    先看看我遇到的问题: @ResponseBody @RequestMapping("/logout") public Json logout(HttpSession session ...

  4. 【Spring学习笔记-MVC-17】Spring MVC之拦截器

    作者:ssslinppp       1. 拦截器简介及应用场景 2. 拦截器接口及拦截器适配器 3. 运行流程图 正常运行 中断流程 4. 程序实例 控制层: @Controller @Reques ...

  5. java之spring mvc之拦截器

    1. springmvc 中的拦截器是由实现 HandlerInterceptor 或者继承 HandlerInterceptorAdapter 来实现的. 2. 自定义实现一个拦截器的步骤: a). ...

  6. spring mvc中拦截器配置mvc:interceptors

    其实在mvc:interceptors标签中,有两种类型的配置,一种直接配置一个bean(bean和ref归为一类),另一种还要配置上拦截的路径和排除的路径.直接配置的bean那就代表对所有的请求进行 ...

  7. spring mvc 配置文件拦截器过滤url

    最近在用spring mvc拦截器,sprin 版本号4.0.6.RELEASE, <mvc:interceptor> <mvc:mapping path="/admin/ ...

  8. Spring MVC定义拦截器

    拦截器: package sy.Interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http ...

  9. Spring Mvc session拦截器实现

    Spring Mvc拦截器实现session过期跳转到登录页面 配置拦截器 <mvc:interceptors> <mvc:interceptor> <mvc:mappi ...

随机推荐

  1. FastReport.Net使用:[21]表格(Table)控件

    对表格控件的一些常用操作 合并单元格:选择需要合并的单元格(按住Shitf多选),然后在右键菜单中选择[合并单元格].         2.删除/插入行 鼠标移到在行头,当鼠标状态变为向右的箭头时点击 ...

  2. [BZOJ5427]最长上升子序列

    考虑O(n log n)的LIS求法,dp[i]表示到目前为止,长度为i的LIS的末尾最小是多少. 当当前数确定时直接用LIS的求法更新dp数组,当不确定时,由于这个数可以是任意数,所以可以接在任意上 ...

  3. 【静态主席树】POJ2104-K-th Number

    求区间第k大.裸线段树. 莫队版本:☆ #include<iostream> #include<cstdio> #include<cstring> #include ...

  4. Understanding Cache Access

    URL Loading System提供了综合的disk 和 in-memory 策略的请求缓存.使用缓存有利于减少程序对网络的依赖,并且能提高程序的体验. Using the Cache for a ...

  5. Nginx出现504 Gateway Time-out的解决方案

    使用Nginx作为WEB服务器时,经常会遇到504 Gateway Time-out的错误提示.经过研究,基本可以确定多数情况下这个错误与Nginx本身无关,问题的根源在于Nginx将PHP的解析提交 ...

  6. maven搭建企业级多模块项目

    1.创建一个maven项目 选择pom 完成 2.创建模块 项目右键选择module,创建模块.创建子模块 其余的打包时都为jar 地址:https://github.com/LeviFromCN/m ...

  7. 【10.3校内测试【国庆七天乐!】】【DP+组合数学/容斥】【spfa多起点多终点+二进制分类】

    最开始想的暴力DP是把天数作为一个维度所以怎么都没有办法优化,矩阵快速幂也是$O(n^3)$会爆炸. 但是没有想到另一个转移方程:定义$f[i][j]$表示每天都有值的$i$天,共消费出总值$j$的方 ...

  8. bzoj 1015 维护连通块个数,离线并查集

    水. /************************************************************** Problem: 1015 User: idy002 Langua ...

  9. Java高级架构师(一)第40节:更多模块的基本功能和配置

  10. 让ios项目同时支持ARC和非ARC

    ttp://code4app.com/snippets/one/禁止某几个文件用ARC模式编译/502344256803fa246d000000#s0 如果你的绝大部分代码需要ARC,那么就设置项目支 ...