SpringMVC的controller提供了PUT和DELETE的请求方式
来源:互联网 发布:java获取进程端口号 编辑:程序博客网 时间:2024/06/08 14:11
在Restful风格中,现有规定如下:
- GET(SELECT):从服务器查询,可以在服务器通过请求的参数区分查询的方式。
- POST(CREATE):在服务器新建一个资源,调用insert操作。
- PUT(UPDATE):在服务器更新资源,调用update操作。
- DELETE(DELETE):从服务器删除资源,调用delete语句
如果当前url是 http://localhost:8080/User
那么用户只要请求这样同一个URL就可以实现不同的增删改查操作,例如
- http://localhost:8080/User?_method=get&id=1001 这样就可以通过get请求获取到数据库 user表里面 id=1001 的用户信息
- http://localhost:8080/User?_method=post&id=1001&name=zhangsan 这样可以向数据库user 表里面插入一条记录
- http://localhost:8080/User?_method=put&id=1001&name=lisi 这样可以将 user表里面 id=1001 的用户名改为lisi
- http://localhost:8080/User?_method=delete&id=1001 这样用于将数据库user 表里面的id=1001 的信息删除
在springMVC中实现restful风格开发
这里,我通过访问http://127.0.0.1:8080/ssmvc/restful接口的method不同来进入不同的controller方法,并打印返回数据。
- @RequestMapping(value = "/restful",method = RequestMethod.GET)
- public void list(HttpServletRequestrequest,HttpServletResponse response,TestVo vo) throws IOException {
- System.out.println("list被访问,参数:" + vo.toString());
- Map<String,Object> map= newHashMap<String, Object>();
- map.put("params",vo);
- map.put("method",RequestMethod.GET);
- response.getWriter().write(JSON.toJSONString(map));
- }
- /**
- * restful风格insert接口
- * @param request
- * @param response
- * @param vo
- * @throws IOException
- */
- @RequestMapping(value = "/restful",method = RequestMethod.POST)
- public voidupdate(HttpServletRequest request, HttpServletResponse response, TestVo vo) throws IOException {
- System.out.println("update被访问,参数:" + vo.toString());
- Map<String,Object> map= newHashMap<String, Object>();
- map.put("params",vo);
- map.put("method",RequestMethod.POST);
- response.getWriter().write(JSON.toJSONString(map));
- }
- /**
- * restful风格update接口
- * @param request
- * @param response
- * @param vo
- * @throws IOException
- */
- @RequestMapping(value = "/restful",method = RequestMethod.PUT)
- public void add(HttpServletRequest request, HttpServletResponse response, TestVo vo) throws IOException {
- System.out.println("add被访问,参数:" + vo.toString());
- Map<String,Object> map= newHashMap<String, Object>();
- map.put("params",vo);
- map.put("method",RequestMethod.PUT);
- response.getWriter().write(JSON.toJSONString(map));
- }
- /**
- * restful风格delete接口
- * @param request
- * @param response
- * @param vo
- * @throws IOException
- */
- @RequestMapping(value = "/restful/{id}",method = RequestMethod.DELETE)
- public void del(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") String id) throws IOException {
- System.out.println("delete被访问,参数:" + ", id:"+ id);
- Map<String,Object> map= newHashMap<String, Object>();
- map.put("params",id);
- map.put("method",RequestMethod.DELETE);
- response.getWriter().write(JSON.toJSONString(map));
- }
这里要注意一下
1.html表单form中,method没有put、delete。
2.springMVC并不能直接接收到通过put、delete方式传过来的参数。
我这里的解决方式是
1.添加过滤器hiddenHttpMethodFilter,作用是将put和delete的参数获取并重新放入request中,controller便可以直接拿到这些参数。
我们需要在web.xml中配置一个过滤器
<!-- 配置过滤器 将POST请求转换为PUT和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>
在表单上增加一个隐藏表单域,将HiddenHttpMethodFilter里的_method属性改为put或者delete后提交
因为只有form表单才具有post方法,而这个过滤器也只能将post方法转化,get则不行。
比如你要提交一个删除的请求
其Controller里的方法对应的
@RequestMapping
(value=
"deleteById/{id}"
,method =
RequestMethod.DELETE
)
//RESTFUL风格
<a
class
=
"del"
href=
"deleteById/100"
>DELETE</a>
//将ID为100的删除
<form action=
""
method=
"post"
id=
"delForm"
>
<input type=
"hidden"
name=
"_method"
value=
"
DELETE
"
>
</form>
当然这种提交是要写js的
$(function(){
$(
'.del'
).click(function(){
$(
'#delForm'
).attr(
'action'
,
this
.href).submit();
})
})
这段js脚本的意思就是
在点击<a>标签删除的时候将a标签的href赋值给id为delForm的表单的action然后让这个form
表单提交.这就完成了将POST请求转换成DELETE请求,那么PUT请求也可以同样这样做.
阅读全文
0 0
- SpringMVC的controller提供了PUT和DELETE的请求方式
- springMvc下的delete,put请求
- SpringMVC 模拟 PUT 提交和 DELETE 方式的提交
- SpringMvc下实现delete和put请求
- SpringMVC接受PUT和DELETE请求
- SpringMVC添加支持Http的delete、put请求
- SpringMVC添加支持Http的delete、put请求
- springmvc将post请求转化成delete和put,所需要进行的配置
- restful风格中的put请求和delete请求的用法
- SpringMVC处理put、delete请求
- Http服务器的交互请求 GET,POST,PUT和DELETE
- RestTemplate的get和post,put,delete请求
- get,post,delete,put的http请求。
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- ASIHTTPRequest实现对PUT,DELETE,POST,GET请求方式的处理
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- 解决DELETE PUT请求方式无法提交表单数据的问题
- ES6 Array.from方法用法总结
- BZOJ 1419: Red is good 期望DP
- 基本数据类型 运算符 类型转换
- C指针
- linux 下载神器(aria2)
- SpringMVC的controller提供了PUT和DELETE的请求方式
- 为什么需要字节对对齐
- Android拖动,缩放,自定义内容,控件制作(可拖动缩放RelativeLayout定制)
- mycat实现mysql读写分离
- "mkimage" command not found
- MAC 上安装LibSVM的python接口
- 欢迎使用CSDN-markdown编辑器
- 014. Spring JdbcDaoSupport访问数据库
- window 安装anaconda(内置numpy与scipy) 可以作为科学计算使用