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
- Spring MVC 菜鸟教程 6 GET,POST,PUT,DELETE
- GET/POST/PUT/DELETE
- get/post/put/delete
- Spring Mvc将Get和Post请求转为Delete和Put请求
- Http Get Post put delete
- Http Get Post put delete
- Http Get Post put delete
- Get Post put delete 详解
- Get Post put delete 详解
- HTTP GET POST PUT DELETE
- Http Get Post put delete
- http post get put delete
- Ajax post +get+put+delete
- post get delete put 请求
- RequestMethod -POST 、GET 、PUT 、DELETE
- C#实现GET,PUT,POST,DELETE
- get,put,post,delete含义与区别
- Http之Get,Post,put,delete请求
- opencv学习之寻找凸包,使用多边形包围轮廓
- Java中的代理(Proxies in Java)
- 【黑苹果教程】ELAN,FocalTech 以及 Synaptics 触控板驱动
- 【UOJ#34】 多项式乘法(FFT && NTT)
- 用myeclipse创建maven项目时,生成的项目名中总是包含Maven Webapp的问题
- Spring MVC 菜鸟教程 6 GET,POST,PUT,DELETE
- 简单几句CSS代码,就可以把网站变成黑白色
- ==和equals方法的区别
- flask扩展模块flask-sqlachemy 的使用---mysql数据库
- Levenshtein算法
- 中科燕园gis兼职团队 祝大家2017年工作顺利 万事如意
- POJ 1836 Alignment DP LIS
- hibernate知识点总结
- Linux下C++编译出错原因解析