Java Bean属性读写

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

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

[b]基本属性[/b]
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());
}
}


[b]子属性[/b]
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());
}
}


[b]数组属性[/b]
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]"));
}
}


[b]容器属性[/b]
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]"));
}
}


[b]图属性[/b]
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]"));
}
}


[b]复杂范型[/b]
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"));
}
}


[b]字符串赋值[/b]
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);
}
}


[b]字符数组赋值[/b]
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]"));
}
}


[b]字符数组容器赋值[/b]
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]"));
}
}


[b]表单解析[/b]
HTML表单
<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
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;
}
}

解析
Enumeration<String> iter = (Enumeration<String>)request.getParameterNames();
Person bean = new Person();
while(iter.hasMoreElements())
{
String key = iter.nextElement();
String[] values = request.getParameterValues(key);
Property.setValue(bean, key, values);
}


[b]源代码[/b]
见附件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值