Spring MVC 菜鸟教程 6 GET,POST,PUT,DELETE

来源:互联网 发布:java获取response对象 编辑:程序博客网 时间:2024/04/29 08:14

服务器交互的不同方法

GET,POST,PUT,DELETE

URL资源

HTTP中的PUT,DELETE,POST,GET就对应着对这个URL资源的增,删,改,查4个操作。
GET一般用于获取/查询资源信息,而POST一般用于更新资源信息。

MVC框架设计问题

早期的Web MVC框架设计者们并没有意识到将URL当作抽象的资源来看的重要性,导致一个的问题是传统的Web MVC框架基本上都只支持GET和POST两种HTTP方法,而不支持PUT和DELETE方法。而Spring MVC是全部支持的。

如何使用PUT/DELETE

表单

这里提交了一个_method的参数

<form action="/put" method="post">          <input type="hidden" name="_method" value="PUT" />          <input type="submit" value="submit" />  </form>  

HiddenHttpMethodFilter

SpringMVC 在页面访问要通过此过滤器

/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.web.filter;import java.io.IOException;import java.util.Locale;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import javax.servlet.http.HttpServletResponse;import org.springframework.util.Assert;import org.springframework.util.StringUtils;/** * {@link javax.servlet.Filter} that converts posted method parameters into HTTP methods, * retrievable via {@link HttpServletRequest#getMethod()}. Since browsers currently only * support GET and POST, a common technique - used by the Prototype library, for instance - * is to use a normal POST with an additional hidden form field ({@code _method}) * to pass the "real" HTTP method along. This filter reads that parameter and changes * the {@link HttpServletRequestWrapper#getMethod()} return value accordingly. * * <p>The name of the request parameter defaults to {@code _method}, but can be * adapted via the {@link #setMethodParam(String) methodParam} property. * * <p><b>NOTE: This filter needs to run after multipart processing in case of a multipart * POST request, due to its inherent need for checking a POST body parameter.</b> * So typically, put a Spring {@link org.springframework.web.multipart.support.MultipartFilter} * <i>before</i> this HiddenHttpMethodFilter in your {@code web.xml} filter chain. * * @author Arjen Poutsma * @since 3.0 */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;    }    @Override    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)            throws ServletException, IOException {        String paramValue = request.getParameter(this.methodParam);        //判断method的方式为POST并且_method不为空        if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {            String method = paramValue.toUpperCase(Locale.ENGLISH);            //关键位置,再次包装HttpServletRequest,进行method转换,看下面代码            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;        }        @Override        public String getMethod() {            return this.method;        }    }}

后台打印

@RequestMapping(value="/put",method=RequestMethod.PUT)  public String put() throws Exception{          System.out.println("put");  }  

其他关联项目

  • SpringMVC 菜鸟教程 1 文件上传 配置
    http://blog.csdn.net/je_ge/article/details/53725357

源码地址

https://github.com/je-ge/spring

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!
微信打赏
支付宝打赏

0 0
原创粉丝点击