今天,项目出了问题。 我们项目的用户验证和权限验证信息(licence )设置在http标头中。 百度一下,只有ajax可以设置报头信息。 无法设置表单。 但是,我突然想起springMVC解决了如何提交表单的put,delete的问题,我一动不动了,就模仿了springMVC
起源
项目使用的是SSM框架。 当然,让我们看一下springMVC对向form表单发送put、delete请求问题的解决方案。 springMVC使用筛选器,以便用户只需将隐藏域_method添加到表单中。 例如,以下内容:
uNum:
uPass:
uName:
csId:
uJob:
uStartTime:
rId:
uMail:
uState:
_method的值是你提出的方式。 具体情况大家自己在百度上详细说。
实现
springmvc在web.xml中的配置如下
httpMethodFilter
org.spring framework.web.filter.hiddenhttpmethodfilter
httpMethodFilter
SpringMVC
接下来,让我们看一下springMVC的源代码:
/*
*版权2002-2012 theoriginalauthororauthors。
*
*LicensedundertheApacheLicense,version2.0(the'license );
* youmaynotusethisfileexceptincompliancewiththelicense。
* youmayobtainacopyofthelicenseat
*
* http://www.Apache.org/licenses/license-2.0
*
* unlessrequiredbyapplicablelaworagreedtoinwriting,software
* distributedunderthelicenseisdistributedonan ' asis ' basis、
* withoutwarrantiesorconditionsofanykind,eitherexpressorimplied。
* sethelicenseforthespecificlanguagegoverningpermissionsand
*limitationsundertheLicense。
*/
package org.spring framework.web.filter;
importjava.io.IOException;
importjava.util.Locale;
import javax.servlet.filter chain;
import javax.servlet.servlet exception;
import javax.servlet.http.http servlet request;
import javax.servlet.http.httpservletrequestwrapper;
import javax.servlet.http.http无servlet轮询;
importorg.spring framework.util.assert;
importorg.spring framework.util.string utils;
//*
* { @ link javax.servlet.filter } thatconvertspostedmethodparametersintohttpmethods,
* retrievable via { @ linkhttpservletrequest # get method ().sincebrowserscurrentlyonly
*supportGETandPOST,acommontechnique-usedbytheprototypelibrary,forinstance-
* istouseanormalpostwithanadditionalhiddenformfield ({ @ code _ method } )
*topassthe'real'HTTPmethod
along. This filter reads that parameter and changes* the {@link HttpServletRequestWrapper#getMethod()} return value accordingly.
*
*
The name of the request parameter defaults to {@code _method}, but can be
* adapted via the {@link #setMethodParam(String) methodParam} property.
*
*
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.
* So typically, put a Spring {@link org.springframework.web.multipart.support.MultipartFilter}
* before 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);
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;
}
@Override
public String getMethod() {
return this.method;
}
}
}
重点我们来看他写的HttpMethodRequestWrapper这个内部类,这个类继承HttpServletRequestWrapper,而HttpServletRequestWrapper我进去看了下都是调用更上层的方法自己并没有做什么事情,再往上我就没去看了。我理解的他的原理是:request在得到method时时使用getMethod方法的,所以他重写了getMethod方法,从而可以把_method的值当做method。
那么既然这样,我也可以把_header的值当做header啊,而request获取header的方法是public String getHeader(String name),所以我就写了下面这样的过滤器:
package com.zs.tools;
import java.io.IOException;
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.web.filter.HiddenHttpMethodFilter;
/**
* hhdzh,2017-2-28
* 处理form表单头的过滤器,
* 如果表单有_header字段,可以自动将该字段转为request的header头信息(增加一条头)
* @author it023
*/
public class MyHiddenHttpMethodFilter extends HiddenHttpMethodFilter{
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String header=request.getParameter("_header");
if (header!=null && !header.trim().equals("")) {
HttpServletRequest wrapper = new HttpHeaderRequestWrapper(request,header);
super.doFilterInternal(wrapper, response, filterChain);
}else {
super.doFilterInternal(request, response, filterChain);
}
}
private static class HttpHeaderRequestWrapper extends HttpServletRequestWrapper{
private final String header;
public HttpHeaderRequestWrapper(HttpServletRequest request,String licence) {
super(request);
this.header=licence;
}
@Override
public String getHeader(String name) {
if (name!=null &&
name.equals("licence") &&
super.getHeader("licence")==null) {
return header;
}else {
return super.getHeader(name);
}
}
}
}
然后,在web.xml中配置一下,我是放在HiddenHttpMethodFilter前面的。
httpHeaderFilter
com.zs.tools.MyHiddenHttpMethodFilter
httpHeaderFilter
SpringMVC
结果
结果很成功,具体的代码我不想贴了,请看日志,这一条表示获取到http头的licence(不要在意licence为什么这么简单,那是因为这是测试数据)。
转自http://blog.csdn.net/fe123rarwa14235pp/article/details/58607296