📄 baseformutil.java
字号:
/*
* Created on 2004-3-24
*
*/
package com.esimple.framework.util;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.BeanUtils;
import java.beans.PropertyDescriptor;
import com.esimple.framework.dao.support.*;
import com.esimple.framework.web.action.BaseForm;
/**
* 操作BaseForm的常用方法
* @author steven
*
*/
public class BaseFormUtil {
public static DateRange makeDateRange(BaseForm form) throws ConvertException {
DateRange range = new DateRange(
form.getValueAsString( DateRange.BEGIN_DATE,true ),
form.getValueAsString( DateRange.END_DATE,true ),
form.getValueAsString( DateRange.DATE_COLUMN,true)
);
return range;
}
public static QueryOrder makeOrderClause(BaseForm form, QueryOrder order)
throws Exception {
try{
order= new QueryOrder(
form.getValueAsString( QueryOrder.ORDER_COLUMN,true ),
form.getValueAsString( QueryOrder.ORDER_TYPE,true)
);
}catch(Exception e) {
System.out.println( "intput para not correct in makeOrderClause" );
}
return order;
}
/**
* 将BaseForm中的值设置到指定javaBean的同名属性。
* @param targetBean
* @param form
* @param propertyName
* @param ignoreEmptyString 是否忽略空字符串
* @throws ConvertException
*/
public static void copyProperty(
Object targetBean,
BaseForm form,
String propertyName,
boolean ignoreEmptyString) throws ConvertException {
try{
PropertyDescriptor origDescriptor =
PropertyUtils.getPropertyDescriptor(targetBean,propertyName);
if( origDescriptor == null ) return;
String name = origDescriptor.getName();
if ( PropertyUtils.isWriteable(targetBean, name)) {
Object obj = form.getValue( name );
if( obj == null ) return;
if( (obj.toString()).length() == 0 && ignoreEmptyString ) return;
//date类型,进行转换
obj = convertValue(origDescriptor, obj);
BeanUtils.copyProperty(targetBean,name,obj);
}
}catch(Exception e) {
e.printStackTrace();
throw new ConvertException(e.getMessage());
}
}
/**
* 复制form中的值到指定javaBean的所有同名参数
* @param targetBean
* @param form
* @param ignoreEmptyString 是否忽略空字符串
* @throws ConvertException
*/
public static void copySimpleProperties(
Object targetBean,
BaseForm form,
boolean ignoreEmptyString) throws ConvertException {
try{
PropertyDescriptor origDescriptors[] =
PropertyUtils.getPropertyDescriptors(targetBean);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// if( name.equals("class") ) continue;
if ( PropertyUtils.isWriteable(targetBean, name)) {
Object obj = form.getValue( name );
if( obj == null ) continue;
if( (obj.toString()).length() == 0 && ignoreEmptyString ) continue;
obj = convertValue(origDescriptors[i],obj);
BeanUtils.copyProperty(targetBean,name,obj);
}
}//for end
}catch(Exception e) {
e.printStackTrace();
throw new ConvertException(e.getMessage());
}
}
//进行类型转换,目前只对Date进行转换
private static Object convertValue(
PropertyDescriptor origDescriptor,
Object obj)
throws ConvertException {
if( origDescriptor.getPropertyType() == java.util.Date.class ){
obj = TimeHelper.getDate(obj.toString());
}
if( origDescriptor.getPropertyType() == java.sql.Date.class ){
obj = TimeHelper.getSqlDate(obj.toString());
}
return obj;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -