|
|
@@ -8,9 +8,15 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.net.URLDecoder;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
public class GetHttpParam {
|
|
|
|
|
|
@@ -129,4 +135,74 @@ public class GetHttpParam {
|
|
|
String data = JSON.toJSONString(map);
|
|
|
return data;
|
|
|
}
|
|
|
+
|
|
|
+ public static <T> T parameterMapToBean(HttpServletRequest req, Class<T> clazz) throws ClassNotFoundException {
|
|
|
+ //获得参数的map集合
|
|
|
+ Map<String, String[]> map = req.getParameterMap();
|
|
|
+
|
|
|
+ //获取实体类的对象
|
|
|
+ T bean = null;
|
|
|
+ try {
|
|
|
+ bean = clazz.newInstance();
|
|
|
+ } catch (InstantiationException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取实体类的所有方法
|
|
|
+ Method[] methods = clazz.getDeclaredMethods();
|
|
|
+ //开始遍历实体类方法
|
|
|
+ for (Method method : methods) {
|
|
|
+ //判断方法名是否以set开头
|
|
|
+ if (method.getName().startsWith("set")) {
|
|
|
+ //遍历map集合中的key
|
|
|
+ for (Map.Entry<String, String[]> entry : map.entrySet()) {
|
|
|
+ //判断方法名末尾是否和map的key一致,比较的时候将方法名全部转为小写
|
|
|
+ if (method.getName().toLowerCase().endsWith(entry.getKey().toLowerCase())) {
|
|
|
+ //遍历map中的value值,因为map中是以 k1={v1,v2} ,k2={v3,v4}的形式存储数据的
|
|
|
+ try {
|
|
|
+ for (String e : entry.getValue()) {
|
|
|
+ //获取set方法中的参数类型
|
|
|
+ Class<?>[] parameterTypes = method.getParameterTypes();
|
|
|
+ //因为set方法的参数类型只有一个,所以取第一个
|
|
|
+ Class<?> parameterType = parameterTypes[0];
|
|
|
+ //获得参数类型的名字
|
|
|
+ String simpleName = parameterType.getSimpleName();
|
|
|
+ //比较参数类型名并将获得的String类型的数据转换为对应实体类的数据
|
|
|
+ if ("String".equals(simpleName)) {
|
|
|
+ method.invoke(bean, e);
|
|
|
+ } else if ("Integer".equals((simpleName)) || "int".equals((simpleName))) {
|
|
|
+ int ee = Integer.parseInt(e);
|
|
|
+ method.invoke(bean, ee);
|
|
|
+ } else if ("Double".equals((simpleName)) || "double".equals((simpleName))) {
|
|
|
+ double ee = Double.parseDouble(e);
|
|
|
+ method.invoke(bean, ee);
|
|
|
+ }
|
|
|
+ //其他类型可以添加,以上举出常用的数据类型
|
|
|
+ }
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ //返回对象
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Class getClass(String simpleName) throws ClassNotFoundException {
|
|
|
+ Matcher matcher = Pattern.compile("(?<=\\<L)(\\S+)(?=\\;>)").matcher(simpleName);
|
|
|
+ if (matcher.find()) {
|
|
|
+ String classUrl = matcher.group().replace("/",".");
|
|
|
+ Class c=Class.forName(classUrl);
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|