Key", "Second Value");
}
return (hash);
}
//下面的方法对在common-beanutils 1.6.1中propertyUtils的getMappedproperty方法不起作用,中不调用这些方法,
//而且不支持嵌套的映射属性
//propertyUtils.setMappedproperty(bean, "nested.hash(Fifth Key)", "Fifth Value"); don''t work!!
public Object getHash(String Key){
System.out.println("getHash Key");
return hash.get(Key);
}
public void setHash(String Key,Object value){
System.out.println("setHash Key value ");
hash.put(Key,value);
}
//这是一个简单属性,想在propertyUtils中修改必须有设置器操作
private String sample = null;
public String getSample() {
return sample;
}
public void setSample(String sample){
this.sample = sample;
}
}
数据库中数据项变化不定时如何设计Java Beans(6)
时间:2011-01-19 IBM 龚永生
3.2.2 使用propertyUtils实用类访问扩展属性
下面propertyUtils以五中格式访问扩展属性的代码:
//testpropertyUtils.java
import org.apache..commons.beanutils.*;
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
public class testpropertyUtils extends TestCase {
public propertyUtilsTestCase(String name) {
super(name);
}
/**
* 实例化TestBean
*/
public void setUp() {
bean = new TestBean();
}
/**
* 测试索引属性
*/
public void testSetIndexedValues() {
Object value = null;
//测试数组索引属性
try {
propertyUtils.setproperty(bean,
"dupproperty(0)",
"New 0");
value =
propertyUtils.getproperty(bean,
"dupproperty(0) ");
assertNotNull("Returned new value 0", value);
assertTrue("Returned String new value 0",
value instanceof String);
assertEquals("Returned correct new value 0", "New 0",
(String) value);
} catch (Throwable t) {
fail("Threw " + t);
}
//测试List索引属性
try {
propertyUtils.setproperty(bean,
"listIndexed(0) " ,
"New value");
value =
propertyUtils.getproperty(bean,
" listIndexed(0)");
assertNotNull("Returned new value 0", value);
assertTrue("Returned String new value 0",
value instanceof String);
assertEquals("Returned correct new value 0", " New value ",
(String) value);
} catch (Throwable t) {
fail("Threw " + t);
}
}
/**
* 测试映射属性
*/
public void testSetMappedValues() {
Object value = null;
try {
propertyUtils.setproperty(bean,
|