项目中将客户的信息保存到本地的Cookie中的使用的:由于每次查询时可以从Cookie中获取,如果改变必须保存到Cookie中,同时Cookie中的值仅仅为字符串形式保存在本地磁盘使用所以必须确保值为字符串格式。同时实时转换。
/**
* 从Cookie获取的对象
*
* @param request
*/
@SuppressWarnings("unchecked")
public static VacationOrderConditionConvertor getFieldProperty2VacationOrderConditionConvertor(HttpServletRequest request) {
List<FieldProperty> propertys = getClientVacationSetting(request);
if (CollectionUtils.isNotEmpty(propertys)) {
VacationOrderConditionConvertor convertor = new VacationOrderConditionConvertor();
for (FieldProperty fieldProperty : propertys) {
String propertyName = fieldProperty.getPropertyName();
String propertyValue = fieldProperty.getPropertyValue();
if (StringUtils.isBlank(propertyValue)) {
continue;
}
try {
//获取方法名称
String methodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
System.out.println("method =" + methodName);
//采用Spring的中ReflectionUtils获取方法并调用
if (fieldProperty.getClazz() == String.class) {
Method method = ReflectionUtils.findMethod(convertor.getClass(), methodName, new Class[] { String.class });
method.invoke(convertor, methodName, String.valueOf(propertyValue));
//采用Apache Commons BeanUtils中的 MethodUtils获取方法并调用
} else if (fieldProperty.getClazz() == Long.class) {
MethodUtils.invokeMethod(convertor, methodName, new Object[] { Long.valueOf(propertyValue) });
//采用原始的反射代码s获取方法并调用
} else if (fieldProperty.getClazz() == Enum.class) {
Method method = ReflectionUtils.findMethod(VacationOrderConditionConvertor.class, methodName, new Class[] { Enum.class });
method.invoke(convertor, Enum.valueOf(fieldProperty.getClazz(), propertyValue));
}
//采用Spring的中ReflectionUtils反射处理异常的信息
} catch (InvocationTargetException e) {
ReflectionUtils.handleReflectionException(e);
} catch (NoSuchMethodException e) {
ReflectionUtils.handleReflectionException(e);
} catch (IllegalAccessException e) {
ReflectionUtils.handleReflectionException(e);
}
}
}
return null;
}