您的位置:首页 > 理论基础 > 计算机网络

Spring-web源码解析之Filter-HiddenHttpMethodFilter

2016-02-24 15:22 531 查看
基于4.1.7.RELEASE
就如同它的名字,该类负责解析隐藏的HttpMethod,用了这个Filter之后,你可以在页面上POST时指定_method参数,该Filter会根据参数指定的值将Request包装成为指定的HttpMethod的request。需要注意的有两点
1 必须是POST方式才进行处理
2 可以通过设置methodParam来更改参数名字,默认为_method。
主要代码如下
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);
}
}


根据注意事项1,如果是GET则直接进行下一个Filter处理。如果是POST,_method参数所传递进来的所有值都会被转换为大写。
附包装器代码
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: