2009-08-25 3 views
3

Maps에서 BeanUtils setProperty 메서드를 사용하려면 어떻게해야합니까?지도가있는 BeanUtils

예 :이 메서드는 다음과 같습니다. public void setAddress (String type, Address address); 다음을 사용하여 설정할 수 있습니다. BeanUtils.setProperty (beanObject, "address (home)", addressObject);

하지만 설정하려는 객체가 맵인 경우 가능합니까? 방법?

public static class Bean{ 
    private Map<String, String> data = new HashMap<String, String>(); 
    public Map<String, String> getData(){ 
     return data; 
    } 
    public void setData(final Map<String, String> data){ 
     this.data = data; 
    } 
} 

public static void main(final String[] args) throws Exception{ 
    final Bean bean = new Bean(); 
    // assign the foo key of the data property to the value 'bar' 
    BeanUtils.setProperty(bean, "data(foo)", "bar"); 
    System.out.println(bean.data); 
} 

출력 :

{foo는 = 바}

+1

이 무엇입니까 – fmaste

답변

2

은 ( bar지도 foo의 열쇠입니다)지도 구문 foo(bar)를 사용하여 BeanUtils 사용자 가이드는 다음과 같이 말합니다 : "표준 JavaBeans API의 확장 BeanUtils 패키지는 기본 값이 java.util.Map 인 모든 특성을 "매핑 됨"으로 간주합니다. 당신은 문자열 값 키를 통해 개별 값을 설정하고 가져올 수 있습니다. " 그러나 set (k, v) 메소드를 사용하여 HashMap과 함께 작동시키는 법을 모르겠습니다.
관련 문제