最近在用struts+spring+hibernate做项目,在做类似添加用户之类的功能时免不了在action中出现类似
User user = new User();
user.setXXX();
这样的代码段来给新创建的entitybean付值,为了使代码更简洁,当然主要是为了不让DAO的操作出现在struts部分的代码中,我决定在service层加通用的工具类来做这件事情。代码如下:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.struts.validator.DynaValidatorForm;
import com.test.service.IcreateEntity;
/**
* set the DynaValidatorForm attribute to the entity object
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @param Object entity
* @param DynaValidatorForm addForm
* @return entity Object
*
*
* @author <a href="mailto:aaaajl@gmail.com">jl*/
public class createEntity implements IcreateEntity{
public Object updateValue(Object entity, DynaValidatorForm form) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException
{
Class cobj; // object
Method[] methodArray; // the object declare method
String mehtodName; // the method name
String AttributeName; // the attribute name in form
Method methodBody; // the method body you can invoke
Class[] methodParam; // the method's parameter type
Object[] obj = new Object[1];
int leng;
/**use reflect to obtain the user object*/
cobj = entity.getClass();
//System.out.println(cobj);
/**use reflect to obtain the user's all get method*/
methodArray = cobj.getMethods();
leng = methodArray.length;
//System.out.println(leng);
for(int i=0;i<leng;i++)
{
/**obtain the method name*/
mehtodName = methodArray[i].getName();
AttributeName = mehtodName.substring(3).toLowerCase();
/**judge the method is a setter method or not*/
if(mehtodName.substring(0,3).equals("set")&&!AttributeName.equals("id"))
{
//System.out.println("AttributeName:"+AttributeName);
/**use reflect to obtain the method parameter's type*/
methodParam = methodArray[i].getParameterTypes();
//System.out.println("methodParam"+methodParam);
/**obtain the method body for invoke*/
methodBody = cobj.getMethod(mehtodName, new Class[]{methodParam[0]});
//System.out.println("create method body sucess");
/**generate parameter Array*/
try{
obj[0] = form.getString(AttributeName)==null?"":form.getString(AttributeName).trim();
}catch (Exception e) {
continue;
}
//System.out.println("obj[0]"+obj[0]);
methodBody.invoke(entity,(Object[]) obj);
//System.out.println("set "+AttributeName+" sucess");
}
}
return entity;
}
}
这个类还有一个问题没有解决就是从form中拿到的Attribute类型都是string型,这与dao中的类型无法安全匹配,为了解决这一问题我近期还要写一个类型转换的工具类。欢迎大家来讨论。
User user = new User();
user.setXXX();
这样的代码段来给新创建的entitybean付值,为了使代码更简洁,当然主要是为了不让DAO的操作出现在struts部分的代码中,我决定在service层加通用的工具类来做这件事情。代码如下:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.struts.validator.DynaValidatorForm;
import com.test.service.IcreateEntity;
/**
* set the DynaValidatorForm attribute to the entity object
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @param Object entity
* @param DynaValidatorForm addForm
* @return entity Object
*
*
* @author <a href="mailto:aaaajl@gmail.com">jl*/
public class createEntity implements IcreateEntity{
public Object updateValue(Object entity, DynaValidatorForm form) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException
{
Class cobj; // object
Method[] methodArray; // the object declare method
String mehtodName; // the method name
String AttributeName; // the attribute name in form
Method methodBody; // the method body you can invoke
Class[] methodParam; // the method's parameter type
Object[] obj = new Object[1];
int leng;
/**use reflect to obtain the user object*/
cobj = entity.getClass();
//System.out.println(cobj);
/**use reflect to obtain the user's all get method*/
methodArray = cobj.getMethods();
leng = methodArray.length;
//System.out.println(leng);
for(int i=0;i<leng;i++)
{
/**obtain the method name*/
mehtodName = methodArray[i].getName();
AttributeName = mehtodName.substring(3).toLowerCase();
/**judge the method is a setter method or not*/
if(mehtodName.substring(0,3).equals("set")&&!AttributeName.equals("id"))
{
//System.out.println("AttributeName:"+AttributeName);
/**use reflect to obtain the method parameter's type*/
methodParam = methodArray[i].getParameterTypes();
//System.out.println("methodParam"+methodParam);
/**obtain the method body for invoke*/
methodBody = cobj.getMethod(mehtodName, new Class[]{methodParam[0]});
//System.out.println("create method body sucess");
/**generate parameter Array*/
try{
obj[0] = form.getString(AttributeName)==null?"":form.getString(AttributeName).trim();
}catch (Exception e) {
continue;
}
//System.out.println("obj[0]"+obj[0]);
methodBody.invoke(entity,(Object[]) obj);
//System.out.println("set "+AttributeName+" sucess");
}
}
return entity;
}
}
这个类还有一个问题没有解决就是从form中拿到的Attribute类型都是string型,这与dao中的类型无法安全匹配,为了解决这一问题我近期还要写一个类型转换的工具类。欢迎大家来讨论。