Note that there are some explanatory texts on larger screens.

plurals
  1. POcan beanutil get the property of one field with its native type
    text
    copied!<p>Hi: I am using BeanUtil to get the properties of some bean,then put them to map. However I found that the <code>getProperty()</code> can only return the string value,I wonder if it can return the native(original) type?</p> <p>For example:</p> <h2>Bean:</h2> <pre><code>public class Entity { private String name; private int age; private List&lt;Date&gt; childs = new ArrayList&lt;Date&gt;(); public Entity(String name, int age, List&lt;Date&gt; childs) { this.name = name; this.age = age; this.childs = childs; } public static Entity newInstance() { List&lt;Date&gt; list = new ArrayList&lt;Date&gt;(); list.add(new Date()); list.add(new Date()); return new Entity("Beanutil", 23, list); } //the getter and setter for all the fields } </code></pre> <hr> <pre><code>Then I want to get some field property of it,and I do not exactly know which fields are requried,so I use it this way: public class BeanUtilMain { public static void main(String[] args){ String[] requestPro={"name","childs"}; //this field is specified by clien Map&lt;String, Object&gt; map=new HashMap&lt;String, Object&gt;(); Entity en=Entity.newInstance(); for(String p:requestPro){ map.put(p, BeanUtils.getProperty(en, p)); } System.out.println(map); } } </code></pre> <p>Then the map is:</p> <pre><code>{name=beanutil, childs=Wed Dec 01 12:24:37 CST 2010} </code></pre> <p>The type of "childs" field is java.util.List,but in the example, it is converted to java.lang.String.</p> <p>I have to hold the orignal type of the fields in the map.</p> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload