Spring MVC restful风格之put and delete
来源:互联网 发布:mac 看文件路径 编辑:程序博客网 时间:2024/05/18 13:43
由于浏览器不支持PUT和DELETE方法,那么我们如何通过Spring实现PUT和DELETE的rest风格呢?
步骤一:
在web.xml中添加一个Spring的过滤器HiddenHttpMethodFilter,对DispatcherServlet进行预处理
<!-- spring mvc 实现delete、put的restful风格过滤器 start--><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name> <!-- DispatcherServlet 对应的servlet-name--> <servlet-name>dispatcher</servlet-name> </filter-mapping> <!-- spring mvc 实现delete、put的restful风格过滤器 end-->步骤二:
向后台提交请求时,将请求类型设置为“POST”,同时向后台传递一个参数"_method",参数值可以设置为PUT或DELETE
Demo:ajax 删除restful风格
deleteById:function(id,callback){//基础验证var url = controllerUtil.controllers.deleteById;if(url.trim().length<=0){throw new Error("please init controllerUtil.controllers.deleteById first.");}console.log("id:"+id);$.ajax({url : url+"/"+id,type : "POST",data : {_method:"DELETE"},dataType : "json",async : true,success : function(ajaxResult) {callback(ajaxResult);},error : function(error) {alert("error");}});}
/** * 删除节点 * @param node * @return */ @RequestMapping(value="/node/{id}",method=RequestMethod.DELETE) @ResponseBody public AjaxResult deleteTreeNode(@PathVariable("id")Long id) { getService().deleteById(id); return AjaxResult.getInstance(); }
以下是Spring过滤器的实现:
public class HiddenHttpMethodFilter extends OncePerRequestFilter {/** Default method parameter: {@code _method} */public static final String DEFAULT_METHOD_PARAM = "_method";private String methodParam = DEFAULT_METHOD_PARAM;/** * Set the parameter name to look for HTTP methods. * @see #DEFAULT_METHOD_PARAM */public void setMethodParam(String methodParam) {Assert.hasText(methodParam, "'methodParam' must not be empty");this.methodParam = methodParam;}@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException {String paramValue = request.getParameter(this.methodParam);if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {String method = paramValue.toUpperCase(Locale.ENGLISH);HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);filterChain.doFilter(wrapper, response);}else {filterChain.doFilter(request, response);}}/** * Simple {@link HttpServletRequest} wrapper that returns the supplied method for * {@link HttpServletRequest#getMethod()}. */private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {private final String method;public HttpMethodRequestWrapper(HttpServletRequest request, String method) {super(request);this.method = method;}@Overridepublic String getMethod() {return this.method;}}}
0 0
- Spring MVC restful风格之put and delete
- Spring MVC使用RESTful风格的PUT请求
- spring mvc RestFul风格PUT方法修改上传文件
- Spring MVC REST风格实现PUT、DELETE请求
- Spring MVC Restful风格
- restful风格中的put请求和delete请求的用法
- Restful风格中put请求和delete请求
- Tomcat默认不支持restful风格的put与delete请求
- Spring MVC RestFul 中的 DELETE 传输方式
- Spring MVC 示例-注解; RESTful 风格
- 基于Restful风格实现Spring MVC
- Spring MVC 支持RestFul风格尝试
- Spring MVC 示例-注解; RESTful 风格
- spring mvc完成restful风格的url
- Ajax中Put和Delete请求传递参数无效的解决方法(Restful风格)
- Ajax中Put和Delete请求传递参数无效的解决方法(Restful风格
- Ajax中Put和Delete请求传递参数无效的解决方法(Restful风格)
- 实现RestFul风格的delete和Put方法的注意点
- dfb 基础和相关的非常用的几个链接
- 深度学习算法--卷积神经网络
- .net rdlc size不对
- Linkedlist模仿栈
- 不折行,撑出隐藏,文字居中,上下居中,输入框和提示信息放一行
- Spring MVC restful风格之put and delete
- 【Win32笔记二】视窗与消息机制
- 第7周项目2-建立链队算法库
- MFC中窗口刷新函数详解
- 第7周项目2-建立链队算法库
- Java Static关键字详解
- springmvc总结
- 第七周-项目三--负数把正数赶出队列
- mac 安装ruby 以及安装vim插件(包含vim)