RESTful架构与SpringMVC框架的结合使用以及PUT、DELETE
来源:互联网 发布:mathematica mac 编辑:程序博客网 时间:2024/06/13 14:17
步骤:
- 在web.xml文件配置过滤器HiddenHttpMethodFilter
- 在controller中设置与调用
详细讲解:
配置过滤器HiddenHttpMethodFilter
<filter> <filter-name>methodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>methodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
关注源码中的doFilterInternal
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException {HttpServletRequest requestToUse = request;if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {String paramValue = request.getParameter(this.methodParam);if (StringUtils.hasLength(paramValue)) {requestToUse = new HttpMethodRequestWrapper(request, paramValue);}}filterChain.doFilter(requestToUse, response);}
里面高亮的代码就是获取请求方式,默认是get而这个methodParam就是通过前端参数传过来的值来判断PUT、DELETE的
以下是前端页面表格中_method的值,对应的是DELETE
<form action="springmvc/testRest/1" method="post"><input type="hidden" name="_method" value="DELETE"><input type="submit" value="restDelete"></form>
在SpringMVC中调用
@Controller@RequestMapping("/springmvc")public class Hello {private static final String SUCCESS = "success";/** * 最终的视图: * prefix+returnValue+suffix * /WEB-INF/views/success.jsp *//** * https://www.douyu.com/485503 restful风格 * {roomId}占位符 * 两种命名情况 * 1.参数的名字和占位符的名字保持一致 * 2.@PathVariable中的名字和占位符中的名字一致,参数的名字就任意 * @return *//** * /testRest POST 新增 * /testRest/1 GET 获取 * /testRest/1 PUT 更新 * /testRest/1 DELETE 删除 * * @param id * @return */@RequestMapping("/testRest/{id}")public String testRestGet(@PathVariable Integer id){System.out.println("testRestGet:"+id);return SUCCESS;}@RequestMapping(value = "/testRest",method = RequestMethod.POST)public String testRestPost(){System.out.println("testRestPost:");return SUCCESS;}@RequestMapping(value = "/testRest/{id}",method = RequestMethod.PUT)public String testRestPut(@PathVariable Integer id){System.out.println("testRestPut:"+id);return SUCCESS;}@RequestMapping(value = "/testRest/{id}",method = RequestMethod.DELETE)public String testRestDelete(@PathVariable Integer id){System.out.println("testRestDelete:"+id);return SUCCESS;}}
阅读全文
0 0
- RESTful架构与SpringMVC框架的结合使用以及PUT、DELETE
- Tomcat默认不支持restful风格的put与delete请求
- springMvc实现restful解决put和delete表单提交问题
- springMvc下的delete,put请求
- springMVC系列(七)——springMVC实现restful风格开发(post、get、put、delete)
- springMVC系列(七)——springMVC实现restful风格开发(post、get、put、delete)
- restful风格中的put请求和delete请求的用法
- springmvc restful put delete Ajax 请求参数取不到-已解决
- 【SpringMVC】用restful风格接口写DELETE或PUT请求时遇到405错误
- springMVC使用PUT、DELETE方法传递参数解决方案
- SpringMVC处理put、delete请求
- springMVC ,put,delete方法失效
- REST架构下,浏览器怎么发送put与delete请求?
- SpringMVC添加支持Http的delete、put请求
- SpringMVC添加支持Http的delete、put请求
- SpringMVC 模拟 PUT 提交和 DELETE 方式的提交
- PHP curl 实现RESTful PUT DELETE 实例
- ASIHttprequest 使用PUT DELETE
- 简单工厂模式
- socket阻塞与非阻塞,同步与异步、I/O模型
- JSP的介绍
- Glide页面加载刷新重新加载,以及刷新页面图片变形的问题
- C语言基础知识
- RESTful架构与SpringMVC框架的结合使用以及PUT、DELETE
- QT实现CSDN上传资源管理助手Demo之(3)请求上传资源页面并解析
- HBase数据导入----improttsv&Bulk Load
- 初识linux
- SQL过滤相同记录
- Ajax跳转与传值
- Hanoi汉诺塔问题(java)
- <input> 格式限定(只能输入中文或者数值等)
- 安卓界面适配