Spring MVC 使用拦截器 HiddenHttpMethodFilter配置Rest风格的URL
<!-- 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的更多相关文章
- Spring mvc登录拦截器
自己实现的第一个Spring mvc登录拦截器 题目要求:拒绝未登录用户进入系统,只要发现用户未登录,则将用户请求转发到/login.do要求用户登录 实现步骤: 1.在spring的配置文件中添加登 ...
- 玩转spring MVC(七)----拦截器
继续在前边的基础上来学习spring MVC中拦截器的使用,下面通过一个例子来实现(完整项目在这里下载:http://download.csdn.net/detail/u012116457/84334 ...
- spring mvc +cookie+拦截器功能 实现系统自动登陆
先看看我遇到的问题: @ResponseBody @RequestMapping("/logout") public Json logout(HttpSession session ...
- 【Spring学习笔记-MVC-17】Spring MVC之拦截器
作者:ssslinppp 1. 拦截器简介及应用场景 2. 拦截器接口及拦截器适配器 3. 运行流程图 正常运行 中断流程 4. 程序实例 控制层: @Controller @Reques ...
- java之spring mvc之拦截器
1. springmvc 中的拦截器是由实现 HandlerInterceptor 或者继承 HandlerInterceptorAdapter 来实现的. 2. 自定义实现一个拦截器的步骤: a). ...
- spring mvc中拦截器配置mvc:interceptors
其实在mvc:interceptors标签中,有两种类型的配置,一种直接配置一个bean(bean和ref归为一类),另一种还要配置上拦截的路径和排除的路径.直接配置的bean那就代表对所有的请求进行 ...
- spring mvc 配置文件拦截器过滤url
最近在用spring mvc拦截器,sprin 版本号4.0.6.RELEASE, <mvc:interceptor> <mvc:mapping path="/admin/ ...
- Spring MVC定义拦截器
拦截器: package sy.Interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http ...
- Spring Mvc session拦截器实现
Spring Mvc拦截器实现session过期跳转到登录页面 配置拦截器 <mvc:interceptors> <mvc:interceptor> <mvc:mappi ...
随机推荐
- 安卓 listView 优化
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_m ...
- windows远程连接Ubuntu12.04 (Ubuntu14.0连接方式不一样)
参考 http://www.cnblogs.com/jerome-rong/archive/2012/08/16/2642035.html 有两种方式:Vnc方式(优缺点)和xrdp模式 中提到的Vn ...
- bzoj 1115: [POI2009]石子游戏Kam -- 博弈论
1115: [POI2009]石子游戏Kam Time Limit: 10 Sec Memory Limit: 162 MB Description 有N堆石子,除了第一堆外,每堆石子个数都不少于前 ...
- 在windows上安装和启动Elasticseach、Kibana
写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...
- FreeRTOS Customisation -- FreeRTOSConfig.h
http://www.freertos.org/a00110.html FreeRTOS is customised using a configuration file called FreeRTO ...
- 利用AWR 查看SQL 执行计划
在AWR中定位到问题SQL语句后想要了解该SQL statement的具体执行计划,于是就用AWR报告中得到的SQL ID去V$SQL等几个动态性能视图中查询,但发现V$SQL或V$SQL_PLAN视 ...
- 添加引用方式抛出和捕获干净的WebService异常
转载:http://www.cnblogs.com/ahdung/p/3953431.html 说明:[干净]指的是客户端在捕获WebService(下称WS)抛出的异常时,得到的ex.Message ...
- C语言跟内存分配方式-alloc malloc calloc
转载:http://blog.csdn.net/ubuntulover/article/details/7581317 (1) 从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整 ...
- 【资料】wod地城掉落
•林中强盗第三层樵夫的皮带子蓝色毡帽面包刀(必定掉) •前往米勒巴赫的旅程第一层木棒邪恶强盗披风邪恶强盗匕首第二层地精盾第三层一瓶啤酒(必定掉大啤酒杯(必定掉 •喧嚷的酒吧第一层指挥棒(传奇等级10旅 ...
- 【docker】【mysql】docker安装mysql,阿里云docker镜像加速器,docker搜索查看远程仓库上的镜像,docker拉取镜像,查看本地所有镜像,查看容器的运行状况,查看容器的详细信息
在docker上安装mysql有两种方式 1.通过Dockerfile构建 2.直接在docker hub上拉取镜像安装 =================本篇采用方法2=============== ...