Vo单级设置

这里主要用到对象的反射机制,运用反射机制调用对象方法,成员,设置属性等

 **********************************

public class EmpAction(){

Emp emp = new Emp;

  public  void setValue(String value){

   return ObjectBeanUtil.setBeanValue(this,value);

}

  public Emp getEmp(){

    return emp;

}

**************************************

public class ObjectBeanUtil{

  public static void setBeanValue(Object actionObject,String msg){  

    String result[] = msg.spit("\\|");

    for(int x=0;x<resultl.length;x++){

      String temp[] = result[x].spilt(":");

      String attribute = temp[0];

      String value = temp[1];

      String fields[] = attribute.spilt("\\.");

      Object wrapObject = ObejctUtil.getObject(actionObject,fields[0]);

      Object obj = ObjectUtil.setBeanValue(wrapAction,fields[1],value);

}

 

}

}

****************************************

public class ObjectUtil{

  public static Object getObject(Object  wrapObject,String value){

    String methodName = "get"+MethodUtil.initcap(value);

    Field field = wrapObject.getClass().getDeclarefile(value);

    if(field==null){

      field = wrapObject.getClass().getField();

    }

    Method method = wrapObject.getClass().getMethod(methodNam);

    method.invoke(wrapObject);

}

public static Object setBeanValue(Object actionObject,String attribute,String value){

    String methodName = "set"+MethodUtil.initcap(attribute);

    Field field = wrapObject.getClass().getDeclarefile(attribute);

    if(field==null){

      field = wrapObject.getClass().getField();

    }

    Method method = wrapObject.getClass().getMethod(methodNam,field.getType());

    method.invoke(wrapObject,value);

}

}

****************************************

public class MethodUtil{

public static String  initcap(String attribute)

  return attribute.substring(0,1).toUppercase()+attribute.substring(1);

}

}

 }

 

 

******************************************

 

TestDemo

String value  = "emp.name:simth|emp.job:heishehui";

EmpAction emp = new EmpAction();

emp.setValue(value);

System.out.Println(emp.getEmp());

 

posted on 2018-03-29 17:06  风中少年2018  阅读(192)  评论(0)    收藏  举报

导航