最近做项目,发现要修改request的参数内容。因为想要在request的paramMap里面默认注入,modifier和modifierName,这些内容。但是这个Map是不能修改的。所以采用了如下办法:下面的办法网上也有的这里只做备份。
1. 创建request转换器
package com.tmall.lafite.web.base; import java.util.Enumeration; import java.util.Map; import java.util.Vector; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; class ParameterRequestWrapper extends HttpServletRequestWrapper { private Map<String, String[]> params; public ParameterRequestWrapper(HttpServletRequest request, Map<String, String[]> newParams) { super(request); this.params = newParams; // RequestDispatcher.forward parameter renewParameterMap(request); } @Override public String getParameter(String name) { String result = ""; Object v = params.get(name); if (v == null) { result = null; } else if (v instanceof String[]) { String[] strArr = (String[]) v; if (strArr.length > 0) { result = strArr[0]; } else { result = null; } } else if (v instanceof String) { result = (String) v; } else { result = v.toString(); } return result; } @Override public Map<String, String[]> getParameterMap() { return params; } @Override public Enumeration<String> getParameterNames() { return new Vector<String>(params.keySet()).elements(); } @Override public String[] getParameterValues(String name) { String[] result = null; Object v = params.get(name); if (v == null) { result = null; } else if (v instanceof String[]) { result = (String[]) v; } else if (v instanceof String) { result = new String[] { (String) v }; } else { result = new String[] { v.toString() }; } return result; } private void renewParameterMap(HttpServletRequest req) { String queryString = req.getQueryString(); if (queryString != null && queryString.trim().length() > 0) { String[] params = queryString.split("&"); for (int i = 0; i < params.length; i++) { int splitIndex = params[i].indexOf("="); if (splitIndex == -1) { continue; } String key = params[i].substring(0, splitIndex); if (!this.params.containsKey(key)) { if (splitIndex < params[i].length()) { String value = params[i].substring(splitIndex + 1); this.params.put(key, new String[] { value }); } } } } } }
2. 转换filter
package com.tmall.lafite.web.base; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class ParamFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { Map<String, String[]> m = new HashMap<String, String[]>( request.getParameterMap()); request = new ParameterRequestWrapper((HttpServletRequest) request, m); chain.doFilter(request, response); } @Override public void destroy() { // TODO Auto-generated method stub } }
3. web.xml
<!-- request参数转化器 --> <filter> <filter-name>ParameterFilter</filter-name> <filter-class>com.tmall.lafite.web.base.ParamFilter</filter-class> </filter> <filter-mapping> <filter-name>ParameterFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
本文转自:http://blog.csdn.net/a9529lty/article/details/25478989