Java Bean属性读写_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3470 | 回复: 0   主题: Java Bean属性读写        下一篇 
jfl
注册用户
等级:少校
经验:1112
发帖:95
精华:0
注册:2012-8-10
状态:离线
发送短消息息给jfl 加好友    发送短消息息给jfl 发消息
发表于: IP:您无权察看 2012-8-28 9:26:38 | [全部帖] [楼主帖] 楼主

属性表达式
使用字符串名称来读取或设置对象属性的方式,使用“.”表示子属性,使用“[]”表示数组、图或者集合索引属性。

功能
支持多级属性
支持数组和容器索引属性
支持范型
支持字符串自动解析属性赋值
支持字符数组属性赋值

基本属性

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. publicclass Person 
  2.       privateint value; 
  3.       publicint getValue() 
  4.        { 
  5.             return value; 
  6.        } 
  7.       publicvoid setValue(int value) 
  8.        { 
  9.             this.value = value; 
  10.        } 
  11. publicclass PersonSample 
  12.       publicstaticvoid main(String ... args) throws Throwable 
  13.        { 
  14.              Person bean = new Person(); 
  15.              Property.setProperty(bean, "value", 123); 
  16.              System.out.println(bean.getValue()); 
  17.        } 


public class Person
{
       private int value;
       public int getValue()
       {
             return value;
       }
       public void setValue(int value)
       {
             this.value = value;
       }
}
public class PersonSample
{
public static void main(String ... args) throws Throwable
{
       Person bean = new Person();
       Property.setProperty(bean, "value", 123);
       System.out.println(bean.getValue());
}
}



子属性

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. publicclass Person 
  2.       privateint value; 
  3.       publicint getValue() 
  4.        { 
  5.             return value; 
  6.        } 
  7.       publicvoid setValue(int value) 
  8.        { 
  9.             this.value = value; 
  10.        } 
  11.       private Person parent; 
  12.       public Person getParent() 
  13.        { 
  14.             return parent; 
  15.        } 
  16.       publicvoid setParent(Person parent) 
  17.        { 
  18.             this.parent = parent; 
  19.        } 
  20. publicclass PersonSample 
  21.       publicstaticvoid main(String ... args) throws Throwable 
  22.        { 
  23.              Person bean = new Person(); 
  24.              Property.setProperty(bean, "parent.parent.value", 123); 
  25.              System.out.println(bean.getParent().getParent().getValue()); 
  26.        } 


public class Person
{
       private int value;
       public int getValue()
       {
             return value;
       }
       public void setValue(int value)
       {
             this.value = value;
       }
       private Person parent;
       public Person getParent()
       {
             return parent;
       }
       public void setParent(Person parent)
       {
             this.parent = parent;
       }
}
public class PersonSample
{
public static void main(String ... args) throws Throwable
{
       Person bean = new Person();
       Property.setProperty(bean, "parent.parent.value", 123);
       System.out.println(bean.getParent().getParent().getValue());
}
}



数组属性

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. publicclass Person 
  2.       privateint[] values; 
  3.       publicint[] getValues() 
  4.        { 
  5.             return values; 
  6.        } 
  7.       publicvoid setValues(int[] values) 
  8.        { 
  9.             this.values = values; 
  10.        } 
  11.       publicstaticvoid main(String ... args) throws Throwable 
  12.        { 
  13.              Person bean = new Person(); 
  14.              Property.setProperty(bean, "values[0]", 123); 
  15.              Property.setProperty(bean, "values[3]", 456); 
  16.              System.out.println(bean.getValues().length); 
  17.             for (int i = 0;i < bean.getValues().length;i ++) 
  18.              { 
  19.                    System.out.println(bean.getValues()[i]); 
  20.              } 
  21.              System.out.println(Property.getProperty(bean, "values[3]")); 
  22.        } 


public class Person
{
       private int[] values;
       public int[] getValues()
       {
             return values;
       }
       public void setValues(int[] values)
       {
             this.values = values;
       }
       public static void main(String ... args) throws Throwable
       {
             Person bean = new Person();
             Property.setProperty(bean, "values[0]", 123);
             Property.setProperty(bean, "values[3]", 456);
             System.out.println(bean.getValues().length);
             for (int i = 0;i < bean.getValues().length;i ++)
             {
                   System.out.println(bean.getValues()[i]);
             }
             System.out.println(Property.getProperty(bean, "values[3]"));
       }
}



容器属性

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. publicclass Person 
  2.       private ArrayList<String> values; 
  3.       public ArrayList<String> getValues() 
  4.        { 
  5.             return values; 
  6.        } 
  7.       publicvoid setValues(ArrayList<String> values) 
  8.        { 
  9.             this.values = values; 
  10.        } 
  11.       publicstaticvoid main(String ... args) throws Throwable 
  12.        { 
  13.              Person bean = new Person(); 
  14.              Property.setProperty(bean, "values[0]", "123"); 
  15.              Property.setProperty(bean, "values[3]", "456"); 
  16.              System.out.println(bean.getValues().size()); 
  17.             for (int i = 0;i < bean.getValues().size();i ++) 
  18.              { 
  19.                    System.out.println(bean.getValues().get(i)); 
  20.              } 
  21.              System.out.println(Property.getProperty(bean, "values[3]")); 
  22.        } 


public class Person
{
       private ArrayList<String> values;
       public ArrayList<String> getValues()
       {
             return values;
       }
       public void setValues(ArrayList<String> values)
       {
             this.values = values;
       }
       public static void main(String ... args) throws Throwable
       {
             Person bean = new Person();
             Property.setProperty(bean, "values[0]", "123");
             Property.setProperty(bean, "values[3]", "456");
             System.out.println(bean.getValues().size());
             for (int i = 0;i < bean.getValues().size();i ++)
             {
                   System.out.println(bean.getValues().get(i));
             }
             System.out.println(Property.getProperty(bean, "values[3]"));
       }
}



图属性

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. publicclass Person 
  2.       private HashMap<String, Double> values; 
  3.       public HashMap<String, Double> getValues() 
  4.        { 
  5.             return values; 
  6.        } 
  7.       publicvoid setValues(HashMap<String, Double> values) 
  8.        { 
  9.             this.values = values; 
  10.        } 
  11.       publicstaticvoid main(String ... args) throws Throwable 
  12.        { 
  13.              Person bean = new Person(); 
  14.              Property.setProperty(bean, "values[abc]", 123.123); 
  15.              Property.setProperty(bean, "values[def]", 456.456); 
  16.              System.out.println(bean.getValues().size()); 
  17.              Set<String> keys = bean.values.keySet(); 
  18.             for (String key : keys) 
  19.              { 
  20.                    System.out.println(bean.values.get(key)); 
  21.              } 
  22.              System.out.println(Property.getProperty(bean, "values[abc]")); 
  23.        } 


public class Person
{
       private HashMap<String, Double> values;
       public HashMap<String, Double> getValues()
       {
             return values;
       }
       public void setValues(HashMap<String, Double> values)
       {
             this.values = values;
       }
       public static void main(String ... args) throws Throwable
       {
             Person bean = new Person();
             Property.setProperty(bean, "values[abc]", 123.123);
             Property.setProperty(bean, "values[def]", 456.456);
             System.out.println(bean.getValues().size());
             Set<String> keys = bean.values.keySet();
             for (String key : keys)
             {
                   System.out.println(bean.values.get(key));
             }
             System.out.println(Property.getProperty(bean, "values[abc]"));
       }
}



复杂范型

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. publicclass Person 
  2.       privatedouble value; 
  3.       private Person parent; 
  4.       private LinkedList<ArrayList<HashMap<Date, Person[][]>>>[] values; 
  5.       public LinkedList<ArrayList<HashMap<Date, Person[][]>>>[] getValues() 
  6.        { 
  7.             return values; 
  8.        } 
  9.       publicvoid setValues(LinkedList<ArrayList<HashMap<Date, Person[][]>>>[] values) 
  10.        { 
  11.             this.values = values; 
  12.        } 
  13.       public Person getParent() 
  14.        { 
  15.             return parent; 
  16.        } 
  17.       publicvoid setParent(Person parent) 
  18.        { 
  19.             this.parent = parent; 
  20.        } 
  21.       publicdouble getValue() 
  22.        { 
  23.             return value; 
  24.        } 
  25.       publicvoid setValue(double value) 
  26.        { 
  27.             this.value = value; 
  28.        } 
  29.       publicstaticvoid main(String ... args) throws Throwable 
  30.        { 
  31.              Person bean = new Person(); 
  32.              Property.setProperty(bean, "values[0][1][2][2012-04-27][3][4].parent.value", 123.456); 
  33.              Property.setProperty(bean, "values[0][1][3][2012-04-27][4][5].parent.value", 456.789); 
  34.              System.out.println(Property.getProperty(bean, "values[0][1][2][2012-04-27][3][4].parent.value")); 
  35.              Date dt = Property.toDate("2012-04-27"); 
  36.              System.out.println(bean.values[0].get(1).get(2).get(dt)[3][4].parent.value); 
  37.              System.out.println(Property.getProperty(bean, "values[0][1][3][2012-04-27][4][5].parent.value")); 
  38.        } 


public class Person
{
       private double value;
       private Person parent;
       private LinkedList<ArrayList<HashMap<Date, Person[][]>>>[] values;
       public LinkedList<ArrayList<HashMap<Date, Person[][]>>>[] getValues()
       {
             return values;
       }
       public void setValues(LinkedList<ArrayList<HashMap<Date, Person[][]>>>[] values)
       {
             this.values = values;
       }
       public Person getParent()
       {
             return parent;
       }
       public void setParent(Person parent)
       {
             this.parent = parent;
       }
       public double getValue()
       {
             return value;
       }
       public void setValue(double value)
       {
             this.value = value;
       }
       public static void main(String ... args) throws Throwable
       {
             Person bean = new Person();
             Property.setProperty(bean, "values[0][1][2][2012-04-27][3][4].parent.value", 123.456);
             Property.setProperty(bean, "values[0][1][3][2012-04-27][4][5].parent.value", 456.789);
             System.out.println(Property.getProperty(bean, "values[0][1][2][2012-04-27][3][4].parent.value"));
             Date dt = Property.toDate("2012-04-27");
             System.out.println(bean.values[0].get(1).get(2).get(dt)[3][4].parent.value);
             System.out.println(Property.getProperty(bean, "values[0][1][3][2012-04-27][4][5].parent.value"));
       }
}



字符串赋值

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. publicclass Person 
  2.       privateint value; 
  3.       publicint getValue() 
  4.        { 
  5.             return value; 
  6.        } 
  7.       publicvoid setValue(int value) 
  8.        { 
  9.             this.value = value; 
  10.        } 
  11.       publicstaticvoid main(String ... args) throws Throwable 
  12.        { 
  13.              Person bean = new Person(); 
  14.              Property.setValue(bean, "value", "123"); 
  15.              System.out.println(bean.value); 
  16.              System.out.println(Property.getProperty(bean, "value")); 
  17.        Property.setValue(bean, "value", new String[]{"456", "789"}); 
  18.              System.out.println(bean.value); 
  19.        } 


public class Person
{
       private int value;
       public int getValue()
       {
             return value;
       }
       public void setValue(int value)
       {
             this.value = value;
       }
       public static void main(String ... args) throws Throwable
       {
             Person bean = new Person();
             Property.setValue(bean, "value", "123");
             System.out.println(bean.value);
             System.out.println(Property.getProperty(bean, "value"));
       Property.setValue(bean, "value", new String[]{"456", "789"});
             System.out.println(bean.value);
       }
}



字符数组赋值

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. publicclass Person 
  2.       privatedouble[] values; 
  3.       publicdouble[] getValues() 
  4.        { 
  5.             return values; 
  6.        } 
  7.       publicvoid setValues(double[] values) 
  8.        { 
  9.             this.values = values; 
  10.        } 
  11.       publicstaticvoid main(String ... args) throws Throwable 
  12.        { 
  13.              Person bean = new Person(); 
  14.        Property.setValue(bean, "values", new String[]{"123.456", "456.789", "789.012"}); 
  15.              System.out.println(bean.values.length); 
  16.              System.out.println(bean.values[1]); 
  17.              System.out.println(Property.getProperty(bean, "values[0]")); 
  18.              System.out.println(Property.getProperty(bean, "values[1]")); 
  19.              System.out.println(Property.getProperty(bean, "values[2]")); 
  20.        } 


public class Person
{
       private double[] values;
       public double[] getValues()
       {
             return values;
       }
       public void setValues(double[] values)
       {
             this.values = values;
       }
       public static void main(String ... args) throws Throwable
       {
             Person bean = new Person();
       Property.setValue(bean, "values", new String[]{"123.456", "456.789", "789.012"});
             System.out.println(bean.values.length);
             System.out.println(bean.values[1]);
             System.out.println(Property.getProperty(bean, "values[0]"));
             System.out.println(Property.getProperty(bean, "values[1]"));
             System.out.println(Property.getProperty(bean, "values[2]"));
       }
}



字符数组容器赋值

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. publicclass Person 
  2.       private ArrayList<Boolean> values; 
  3.       public ArrayList<Boolean> getValues() 
  4.        { 
  5.             return values; 
  6.        } 
  7.       publicvoid setValues(ArrayList<Boolean> values) 
  8.        { 
  9.             this.values = values; 
  10.        } 
  11.       publicstaticvoid main(String ... args) throws Throwable 
  12.        { 
  13.              Person bean = new Person(); 
  14.        Property.setValue(bean, "values", new String[]{"1"}); 
  15.              System.out.println(bean.values.size()); 
  16.              System.out.println(Property.getProperty(bean, "values[0]")); 
  17.        Property.setValue(bean, "values", new String[]{"1", "0", "true", "false"}); 
  18.              System.out.println(bean.values.size()); 
  19.              System.out.println(Property.getProperty(bean, "values[0]")); 
  20.              System.out.println(Property.getProperty(bean, "values[1]")); 
  21.        } 


public class Person
{
       private ArrayList<Boolean> values;
       public ArrayList<Boolean> getValues()
       {
             return values;
       }
       public void setValues(ArrayList<Boolean> values)
       {
             this.values = values;
       }
       public static void main(String ... args) throws Throwable
       {
             Person bean = new Person();
       Property.setValue(bean, "values", new String[]{"1"});
             System.out.println(bean.values.size());
             System.out.println(Property.getProperty(bean, "values[0]"));
       Property.setValue(bean, "values", new String[]{"1", "0", "true", "false"});
             System.out.println(bean.values.size());
             System.out.println(Property.getProperty(bean, "values[0]"));
             System.out.println(Property.getProperty(bean, "values[1]"));
       }
}



表单解析
HTML表单

Html代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. <formmethod="POST"action="myAction.do">
  2. <!-- 基本属性 -->
  3. <inputtype="text"name="user">
  4. <inputtype="password"name="pwd">
  5. <!-- 同名复选框,映射到Java Bean的数组 -->
  6. <inputtype="checkbox"name="fruits"value="apple">
  7. <inputtype="checkbox"name="fruits"value="orange">
  8. <inputtype="checkbox"name="fruits"value="banana">
  9. <inputtype="checkbox"name="fruits"value="mango">
  10. <!-- 多选子属性,映射到Java Bean的ArrayList -->
  11. <inputtype="text"name="children[0].user"><inputtype="password"name="children[0].pwd">
  12. <inputtype="text"name="children[1].user"><inputtype="password"name="children[1].pwd">
  13. <inputtype="text"name="children[2].user"><inputtype="password"name="children[2].pwd">
  14. <inputtype="submit"value="提交">
  15. </form>


<form method="POST" action="myAction.do">
<!-- 基本属性 -->
<input type="text" name="user">
<input type="password" name="pwd">
<!-- 同名复选框,映射到Java Bean的数组 -->
<input type="checkbox" name="fruits" value="apple">
<input type="checkbox" name="fruits" value="orange">
<input type="checkbox" name="fruits" value="banana">
<input type="checkbox" name="fruits" value="mango">
<!-- 多选子属性,映射到Java Bean的ArrayList -->
<input type="text" name="children[0].user"><input type="password" name="children[0].pwd">
<input type="text" name="children[1].user"><input type="password" name="children[1].pwd">
<input type="text" name="children[2].user"><input type="password" name="children[2].pwd">
<input type="submit" value="提交">
</form>

Java Bean


Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. publicclass Person 
  2.       private String name; 
  3.       private String pwd; 
  4.       private String[] fruits; 
  5.       private ArrayList<Person> children; 
  6.       public String getName() 
  7.        { 
  8.             return name; 
  9.        } 
  10.       publicvoid setName(String name) 
  11.        { 
  12.             this.name = name; 
  13.        } 
  14.       public String getPwd() 
  15.        { 
  16.             return pwd; 
  17.        } 
  18.       publicvoid setPwd(String pwd) 
  19.        { 
  20.             this.pwd = pwd; 
  21.        } 
  22.       public String[] getFruits() 
  23.        { 
  24.             return fruits; 
  25.        } 
  26.       publicvoid setFruits(String[] fruits) 
  27.        { 
  28.             this.fruits = fruits; 
  29.        } 
  30.       public ArrayList<Person> getChildren() 
  31.        { 
  32.             return children; 
  33.        } 
  34.       publicvoid setChildren(ArrayList<Person> children) 
  35.        { 
  36.             this.children = children; 
  37.        } 


public class Person
{
       private String name;
       private String pwd;
       private String[] fruits;
       private ArrayList<Person> children;
       public String getName()
       {
             return name;
       }
       public void setName(String name)
       {
             this.name = name;
       }
       public String getPwd()
       {
             return pwd;
       }
       public void setPwd(String pwd)
       {
             this.pwd = pwd;
       }
       public String[] getFruits()
       {
             return fruits;
       }
       public void setFruits(String[] fruits)
       {
             this.fruits = fruits;
       }
       public ArrayList<Person> getChildren()
       {
             return children;
       }
       public void setChildren(ArrayList<Person> children)
       {
             this.children = children;
       }
}


解析

Java代码
北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论