属性表达式
使用字符串名称来读取或设置对象属性的方式,使用“.”表示子属性,使用“[]”表示数组、图或者集合索引属性。
功能
支持多级属性
支持数组和容器索引属性
支持范型
支持字符串自动解析属性赋值
支持字符数组属性赋值
基本属性
Java代码
- publicclass Person
- {
- privateint value;
- publicint getValue()
- {
- return value;
- }
- publicvoid setValue(int value)
- {
- this.value = value;
- }
- }
- publicclass PersonSample
- {
- publicstaticvoid main(String ... args) throws Throwable
- {
- Person bean = new Person();
- Property.setProperty(bean, "value", 123);
- System.out.println(bean.getValue());
- }
- }
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代码
- publicclass Person
- {
- privateint value;
- publicint getValue()
- {
- return value;
- }
- publicvoid setValue(int value)
- {
- this.value = value;
- }
- private Person parent;
- public Person getParent()
- {
- return parent;
- }
- publicvoid setParent(Person parent)
- {
- this.parent = parent;
- }
- }
- publicclass PersonSample
- {
- publicstaticvoid main(String ... args) throws Throwable
- {
- Person bean = new Person();
- Property.setProperty(bean, "parent.parent.value", 123);
- System.out.println(bean.getParent().getParent().getValue());
- }
- }
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代码
- publicclass Person
- {
- privateint[] values;
- publicint[] getValues()
- {
- return values;
- }
- publicvoid setValues(int[] values)
- {
- this.values = values;
- }
- publicstaticvoid 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]"));
- }
- }
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代码
- publicclass Person
- {
- private ArrayList<String> values;
- public ArrayList<String> getValues()
- {
- return values;
- }
- publicvoid setValues(ArrayList<String> values)
- {
- this.values = values;
- }
- publicstaticvoid 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]"));
- }
- }
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代码
- publicclass Person
- {
- private HashMap<String, Double> values;
- public HashMap<String, Double> getValues()
- {
- return values;
- }
- publicvoid setValues(HashMap<String, Double> values)
- {
- this.values = values;
- }
- publicstaticvoid 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]"));
- }
- }
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代码
- publicclass Person
- {
- privatedouble value;
- private Person parent;
- private LinkedList<ArrayList<HashMap<Date, Person[][]>>>[] values;
- public LinkedList<ArrayList<HashMap<Date, Person[][]>>>[] getValues()
- {
- return values;
- }
- publicvoid setValues(LinkedList<ArrayList<HashMap<Date, Person[][]>>>[] values)
- {
- this.values = values;
- }
- public Person getParent()
- {
- return parent;
- }
- publicvoid setParent(Person parent)
- {
- this.parent = parent;
- }
- publicdouble getValue()
- {
- return value;
- }
- publicvoid setValue(double value)
- {
- this.value = value;
- }
- publicstaticvoid 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"));
- }
- }
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代码
- publicclass Person
- {
- privateint value;
- publicint getValue()
- {
- return value;
- }
- publicvoid setValue(int value)
- {
- this.value = value;
- }
- publicstaticvoid 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);
- }
- }
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代码
- publicclass Person
- {
- privatedouble[] values;
- publicdouble[] getValues()
- {
- return values;
- }
- publicvoid setValues(double[] values)
- {
- this.values = values;
- }
- publicstaticvoid 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]"));
- }
- }
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代码
- publicclass Person
- {
- private ArrayList<Boolean> values;
- public ArrayList<Boolean> getValues()
- {
- return values;
- }
- publicvoid setValues(ArrayList<Boolean> values)
- {
- this.values = values;
- }
- publicstaticvoid 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]"));
- }
- }
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代码
- <formmethod="POST"action="myAction.do">
- <!-- 基本属性 -->
- <inputtype="text"name="user">
- <inputtype="password"name="pwd">
- <!-- 同名复选框,映射到Java Bean的数组 -->
- <inputtype="checkbox"name="fruits"value="apple">
- <inputtype="checkbox"name="fruits"value="orange">
- <inputtype="checkbox"name="fruits"value="banana">
- <inputtype="checkbox"name="fruits"value="mango">
- <!-- 多选子属性,映射到Java Bean的ArrayList -->
- <inputtype="text"name="children[0].user"><inputtype="password"name="children[0].pwd">
- <inputtype="text"name="children[1].user"><inputtype="password"name="children[1].pwd">
- <inputtype="text"name="children[2].user"><inputtype="password"name="children[2].pwd">
- <inputtype="submit"value="提交">
- </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代码
- publicclass Person
- {
- private String name;
- private String pwd;
- private String[] fruits;
- private ArrayList<Person> children;
- public String getName()
- {
- return name;
- }
- publicvoid setName(String name)
- {
- this.name = name;
- }
- public String getPwd()
- {
- return pwd;
- }
- publicvoid setPwd(String pwd)
- {
- this.pwd = pwd;
- }
- public String[] getFruits()
- {
- return fruits;
- }
- publicvoid setFruits(String[] fruits)
- {
- this.fruits = fruits;
- }
- public ArrayList<Person> getChildren()
- {
- return children;
- }
- publicvoid setChildren(ArrayList<Person> children)
- {
- this.children = children;
- }
- }
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代码