java request参数_java request 动态参数赋值给对象

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 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);

}

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2010-03-01 20:59

浏览 3608

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以通过代理模式来实现对原始对象的代理操作。代理对象可以在不改变原始对象的情况下,增强原始对象的功能。在代理模式中,将代理对象赋值给原始对象可以通过以下步骤实现: 1. 定义接口:定义一个接口,规定代理对象和原始对象需要实现的方法。 2. 实现原始对象:实现原始对象的接口,并在其中定义方法的具体实现。 3. 实现代理对象:实现代理对象的接口,并在其中定义方法的具体实现。在代理对象的方法中,可以调用原始对象的方法,并在调用前后进行一些增强操作。 4. 创建代理对象:创建代理对象,并将其赋值给原始对象。 示例代码如下: ``` // 定义接口 interface Subject { void request(); } // 实现原始对象 class RealSubject implements Subject { @Override public void request() { System.out.println("RealSubject: handling request."); } } // 实现代理对象 class Proxy implements Subject { private RealSubject realSubject; public Proxy() { this.realSubject = new RealSubject(); } @Override public void request() { System.out.println("Proxy: handling request."); realSubject.request(); System.out.println("Proxy: request handled."); } } // 创建代理对象赋值给原始对象 Subject subject = new Proxy(); subject.request(); ``` 在上述代码中,创建了一个RealSubject实现了Subject接口,代表了原始对象。创建了一个Proxy实现了Subject接口,代表了代理对象。在Proxy类中,通过创建RealSubject对象,并在request方法中调用RealSubject的request方法来实现对原始对象的代理操作。最后,创建了一个代理对象并将其赋值给Subject类型的变量subject中,即将代理对象赋值给原始对象。调用subject的request方法即可触发代理操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值