public class ParamUtil {
private static String DATE_FORMAT_1 = "yyyy-MM-dd";
private static String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";
private static String DATE_MATCHER_1 = "^\\d{4}(\\-)\\d{1,2}\\1\\d{1,2}$";
private static String DATE_MATCHER_2 = "^\\d{4}(\\-)\\d{1,2}\\1\\d{1,2}(\\s([0-1]\\d|[2][0-3])\\:[0-5]\\d\\:[0-5]\\d)$";
private static String FORMAT_NAME = "format_";
public static String getFunName(String objName) {
String funName = objName.substring(0, 1).toUpperCase();
if (objName.length() > 1) {
funName += objName.substring(1, objName.length());
}
return funName;
}
/**
* 动态赋值给系统对象 String Integer Long Float Date 等
*
* @param bean 执行对象
* @param propertyName 属性
* @param value 参数
* @param format 参数格式化规格
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws ParseException
*/
public static void bindProperty(Object bean, String propertyName, String value, String format) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ParseException, InstantiationException {
//得到方法名
String funName = getFunName(propertyName);
//get方法
Method getMethod = bean.getClass().getMethod("get" + funName, null);
//得到参数类型
Class cl = getMethod.getReturnType();
//set方法
Method setMethod = bean.getClass().getMethod("set" + funName, new Class[]{cl});
//当参数为空时直接赋予NULL值
if (value.trim().equals("")) {
setMethod.invoke(bean, new Object[]{null});
return;
}
//根据不同的系统对象转换
if (cl == String.class) {
setMethod.invoke(bean, new Object[]{value});
return;
} else if (cl == Integer.class || cl == Float.class || cl == Long.class || cl == Double.class || cl == Byte.class || cl == Boolean.class || cl == Character.class) {
Method valueOf = cl.getMethod("valueOf", new Class[]{String.class});
Object valueObj = valueOf.invoke(cl, new Object[]{value});
setMethod.invoke(bean, new Object[]{valueObj});
} else if (cl == java.util.Date.class || cl == java.sql.Date.class) {
java.text.SimpleDateFormat simpleDateFormat = null;
String formatChar = null;
//判断格式参数存在
if (format != null) {
formatChar = format;
//分别2个默认的格式化规制
} else if (Pattern.compile(DATE_MATCHER_1).matcher(value).find()) {
formatChar = DATE_FORMAT_1;
//分别2个默认的格式化规制
} else if (Pattern.compile(DATE_MATCHER_2).matcher(value).find()) {
formatChar = DATE_FORMAT_2;
}
if (formatChar != null) {
simpleDateFormat = new java.text.SimpleDateFormat(formatChar);
Date date = simpleDateFormat.parse(value);
Object dateObj = cl.newInstance();
Method setTime = cl.getMethod("setTime", new Class[]{long.class});
setTime.invoke(dateObj, new Object[]{date.getTime()});
setMethod.invoke(bean, new Object[]{dateObj});
}
}
}
/**
* 动态赋值给自定义对象
* member.city.id = 110
* bean = member
* objName = city
* propertyName = id
* value = 110
*
* @param bean 执行对象
* @param objName 做参数的对象
* @param propertyName 做参数的对象的属性名称
* @param value 参数
* @param format 参数格式化规格
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws ParseException
* @throws InstantiationException
*/
public static void bindSubObject(Object bean, String objName, String propertyName, String value, String format) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ParseException, InstantiationException {
String funName = getFunName(objName);
Method getMethod = bean.getClass().getMethod("get" + funName, null);
Class cls = getMethod.getReturnType();
Object obj = getMethod.invoke(bean, null);
//判断参数为空,直接设置NULL值.
if (value.trim().equals("")) {
if (obj != null) {
Method setMethod = bean.getClass().getMethod("set" + funName, new Class[]{cls});
setMethod.invoke(bean, new Object[]{null});
}
return;
}
//如果对象未初次化
if (obj == null) {
obj = cls.newInstance();
}
//设置普通系统对象的属性
bindProperty(obj, propertyName, value, format);
Method setMethod = bean.getClass().getMethod("set" + funName, new Class[]{cls});
//把对象填充
setMethod.invoke(bean, new Object[]{obj});
}
/**
* 动态从request.getParameter里绑定javaBean属性值.
*
* @param request http响应对象
* @param bean 绑定的javaBean
* @param excludes 过滤参数
* @param prefix 前缀 只取前缀的
*/
public static void bindData(HttpServletRequest request, Object bean, List excludes, String prefix) {
java.util.Enumeration<String> em = request.getParameterNames();
for (; em.hasMoreElements();) {
try {
String name = em.nextElement();
String value = request.getParameter(name);
if (value == null) {
continue;
}
if (excludes != null && excludes.contains(value)) {
continue;
}
if (prefix != null) {
if (name.indexOf(prefix + ".") != 0) {
continue;
}
name = name.replaceFirst(prefix + ".", "");
}
value = value.trim();
if (name.indexOf(".") > -1) {
int start = name.indexOf(".");
String objName = name.substring(0, start);
String propertyName = name.substring(start + 1, name.length());
bindSubObject(bean, objName, propertyName, value, request.getParameter(FORMAT_NAME + name));
} else {
bindProperty(bean, name, value, request.getParameter(FORMAT_NAME + name));
}
} catch (NoSuchMethodException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ParseException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InstantiationException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
/**
* 动态从request.getParameter里绑定javaBean属性值.
*
* @param request http响应对象
* @param bean 绑定的javaBean
*/
public static void bindData(HttpServletRequest request, Object bean) {
bindData(request, bean, null, null);
}
}
分享到:
相关推荐
17.6.2 request对象 17.6.3 response对象 17.6.4 session对象 17.6.5 application对象 17.6.6 pageContext对象 17.6.7 exception对象 17.6.8 config对象 17.7 计数器 17.8 本章习题 第18章 KTV管理系统 ...
全书分4篇,共24章,其中,第1篇为技能学习篇,主要包括Java Web开发环境、JSP语法、JSP内置对象、Java Bean技术、Servlet技术、EL与JSTL标签库、数据库应用开发、初识Struts2基础、揭密Struts2高级技术、Hib锄劬e...
21、当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递? 10 22、我们在web 应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,...
例如,EL 表达式可以使用 ${requestScope.objectName} 访问一个 JSP 请求范围的对象,还可以使用 ${requestScope.objectName.attributeName} 访问对象的属性。 sessionScope 将会话范围的变量名称映射到其值。该...
10.11.4不能实例化参数类型对象..327 10.12本章小结328 第11章集合329 11.1集合接口与抽象类329 11.1.1Collection接口330 11.1.2Iterator接口330 11.1.3AbstractCollection抽象类331 11.1.4几个常用术语332 ...
值.pageScope表示页面范围的变量.requestScope表示请求对象的变量. sessionScope表示会话 范围内的变量.applicationScope表示应用范围的变量. 3 --<%@ page isELIgnored="true"%> 表示是否禁用EL语言,TRUE...
21、当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递? 10 22、我们在web 应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,...
方法变量通常是基于给出的参数计算值在数据模型中定义。 6、 用户自定义FTL指令:宏和变换器 7、 节点 节点变量表示为树型结构中的一个节点,通常在XML处理中使用。 在模板里对sequences和hashes初始化 ...
GET和POST请求传的参数会自动转换赋值到@RequestParam 所注解的变量上 defaultValue 默认值 required 默认值为true , 当为false时 这个注解可以不传这个参数 null || .size()==0 ResponseEntity...
3.2.2. 高级使用:持久一个 DbTable 结果对象 3.2.3. 高级用法示例 3.3. 摘要式认证 3.3.1. 简介 3.3.2. 规范(Specifics) 3.3.3. 身份(Identity) 3.4. HTTP 认证适配器 3.4.1. 简介 3.4.2. 设计回顾 ...