1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Vector;
/** * @Author: XiongFeng * @Description: 对Request请求重新包装 * @Date: Created in 11:17 2018/4/13 */ public class ParameterRequestWrapper extends HttpServletRequestWrapper {
private Map<String , String[]> params = new HashMap<String, String[]>();
@SuppressWarnings("unchecked") public ParameterRequestWrapper(HttpServletRequest request) { // 将request交给父类,以便于调用对应方法的时候,将其输出,其实父亲类的实现方式和第一种new的方式类似 super(request); //将参数表,赋予给当前的Map以便于持有request中的参数 this.params.putAll(request.getParameterMap()); } //重载一个构造方法 public ParameterRequestWrapper(HttpServletRequest request , Map<String , Object> extendParams) { this(request); addAllParameters(extendParams);//这里将扩展参数写入参数表 }
/** * 复写获取key的方法 */ @Override public Enumeration getParameterNames() { Vector names = new Vector(params.keySet()); return names.elements(); }
/** * 复写获取值value的方法 */ @Override public String getParameter(String name) { Object v = params.get(name); if (v == null) { return null; } else if (v instanceof String[]) { String[] strArr = (String[]) v; if (strArr.length > 0) { return strArr[0]; } else { return null; } } else if (v instanceof String) { return (String) v; } else { return v.toString(); } }
@Override public String[] getParameterValues(String name) { Object v = params.get(name); if (v == null) { return null; } else if (v instanceof String[]) { return (String[]) v; } else if (v instanceof String) { return new String[] { (String) v }; } else { return new String[] { v.toString() }; } }
public void addAllParameters(Map<String , Object>otherParams) {//增加多个参数 for(Map.Entry<String , Object>entry : otherParams.entrySet()) { addParameter(entry.getKey() , entry.getValue()); } }
public void addParameter(String name , Object value) {//增加参数 if(value != null) { if(value instanceof String[]) { params.put(name , (String[])value); }else if(value instanceof String) { params.put(name , new String[] {(String)value}); }else { params.put(name , new String[] {String.valueOf(value)}); } } }
/** 简单封装,请根据需求改进 */ public void addObject(Object obj) { Class<?> clazz = obj.getClass(); Method[] methods = clazz.getMethods(); try { for (Method method : methods) { if (!method.getName().startsWith("get")) { continue; } Object invoke = method.invoke(obj); if (invoke == null || "".equals(invoke)) { continue; }
String filedName = method.getName().replace("get", ""); filedName = WordUtils.uncapitalize(filedName);
if (invoke instanceof Collection) { Collection collections = (Collection) invoke; if (collections != null && collections.size() > 0) { String[] strings = (String[]) collections.toArray(); addParameter(filedName, strings); return; } }
addParameter(filedName, invoke); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
}
|
评论系统未开启,无法评论!