怎样做QQ网站呢,网站增加域名备案,开发网站的基本流程五个阶段,好用的黄页网转自#xff1a; https://blog.csdn.net/cockroach02/article/details/82194126https://blog.csdn.net/cockroach02/article/details/82194126 一、当前现状
浏览器使用form提交信息的时候只支持GET和POST#xff0c;如果需要在浏览器上使用PUT和DELETE请求方式的话#…转自 https://blog.csdn.net/cockroach02/article/details/82194126https://blog.csdn.net/cockroach02/article/details/82194126 一、当前现状
浏览器使用form提交信息的时候只支持GET和POST如果需要在浏览器上使用PUT和DELETE请求方式的话只能使用欺骗的方式了SpringMvc提供了HiddenHttpMethodFilter类来提供支持请看代码
public class HiddenHttpMethodFilter extends OncePerRequestFilter {/** Default method parameter: {code _method} *///我们的隐藏字段name必须为_methodpublic 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 {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);}/*** 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.toUpperCase(Locale.ENGLISH);}//通过继承方式对getMethod方法做了下改变就变成了PUT或者DELETE了Overridepublic String getMethod() {return this.method;}}} 二、配置步骤
1. web.xml !-- HTTP PUT Form --filterfilter-nameHiddenHttpMethodFilter/filter-namefilter-classorg.springframework.web.filter.HiddenHttpMethodFilter/filter-class/filterfilter-mappingfilter-nameHiddenHttpMethodFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping
2. putform.jsp
% page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8%
!DOCTYPE html
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
bodyform methodPOST action%request.getServletContext().getContextPath()%/home/putformBodyinput typehidden name_method valuePUTp姓名/pinput typetext namename /br/p性别/pinput typetext namesex /br/p年龄/pinput typetext nameage /br/button typesubmit提交/button/form
/body
/html
3. putformBodycontroller方法
RequestMapping(pathhome/putformBody, methodRequestMethod.PUT, produces text/plain;charsetutf-8)ResponseBodypublic String putformBody(HttpServletRequest req, HttpServletResponse resp) {String name req.getParameter(name);String sex req.getParameter(sex);String age req.getParameter(age);return name: name ,sex: sex ,age: age;} 四、中间遇到的坑
1、拦截器的url-pattern必须配置为/*不能配置/否则不生效 2、为对中文支持避免乱码配置CharacterEncodingFilter必须为放在第一个否则即使是配置生效断点调试能进去但是依然中文乱码初学的朋友参考web.xml如下
?xml version1.0 encodingUTF-8?
web-app version3.0 xmlnshttp://java.sun.com/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsddescriptioncockroach-springmvc-xml/descriptioncontext-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath*:applicationContext.xml/param-value/context-paramservletservlet-namespringmvc/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/springmvc-servlet.xml/param-value/init-paramload-on-startup1/load-on-startup/servletservlet-mappingservlet-namespringmvc/servlet-nameurl-pattern//url-pattern/servlet-mapping!-- UTF-8 encoding --filterfilter-nameCharacterEncodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-paraminit-paramparam-nameforceEncoding/param-nameparam-valuetrue/param-value/init-param/filterfilter-mappingfilter-nameCharacterEncodingFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping!-- HTTP PUT Form --filterfilter-nameHiddenHttpMethodFilter/filter-namefilter-classorg.springframework.web.filter.HiddenHttpMethodFilter/filter-class/filterfilter-mappingfilter-nameHiddenHttpMethodFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping
/web-app
3、SpringMvc以ResponseBody返回的时中文乱码demo学习的话可以配置下RequestMapping如下
RequestMapping(pathhome/putformBody, methodRequestMethod.PUT, produces text/plain;charsetutf-8) 五 参考连接
如何发送PUT请求和DELETE请求html 对 form 表单中 put,delete,patch的支持HTTP PUT请求时表单数据无法传递springmvc 明明到处都配置了编码为UTF-8可还是乱码