论坛首页 Java企业应用论坛

Java Bean属性读写

浏览 4275 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-04-27  
属性表达式
使用字符串名称来读取或设置对象属性的方式,使用“.”表示子属性,使用“[]”表示数组、图或者集合索引属性。

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

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


子属性
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());
    }
}


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


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


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


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


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


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


字符数组容器赋值
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表单
<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);
}


源代码
见附件
  • property.zip (11.9 KB)
  • 描述: 源代码和文档
  • 下载次数: 68
   发表时间:2012-04-28  
请参考commons  beanutils 和properyutils 类!!
0 请登录后投票
   发表时间:2012-04-28  
自己写一个小框架时自己实现了类型读写,看到这个,感觉不错,另外知道了还有"请参考commons  beanutils 和properyutils 类!!"
这些现有的东西,看来我自己造轮子有点2了。
0 请登录后投票
   发表时间:2012-04-28  
或 参考 spring的BeanWrapperImpl

实现一遍挺好的,能学习很多知识

强烈推荐看看BeanWrapperImpl
0 请登录后投票
   发表时间:2012-04-29  
好东西,如果不是用缓存的话,我想效率应该不会太高吧。
建议加上对方法(Method)的缓存。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics