Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd say it depends on what kind of object the actual <code>domainObject</code> is. If it's a bean, you could use an reflection based automated bean populator such as my <a href="http://code.google.com/p/bean-property-controller/" rel="nofollow noreferrer">BeanPropertyController</a> <em>(it's free so give it a try! License is <a href="http://www.apache.org/licenses/LICENSE-2.0.html" rel="nofollow noreferrer">ASF2.0</a>)</em> to do the following:</p> <pre><code>/* Assumed parameter input: * a=hello * c=123 * e=f,g,h * * Matching bean: */ public class ParamBean { private String a; private int c; private String[] e; /* + all the normal getters and setters you'd see in a bean */ } /* somewhere in your servlet: */ BeanPropertyController bpc = BeanPropertyController.of(ParamBean.class); for (Entry&lt;String, String[]&gt; entry : request.getParameterMap().entrySet()) { bpc.mutate(entry.getKey(), getNiceValue(entry.getValue(), bpc.typeOf(entry.getKey())); } ParamBean bean = bpc.getObject(ParamBean.class); /* somewhere else in the class */ public Object getNiceValue(String[] objs, Class&lt;?&gt; desiredClass) { if (objs.length == 1) { Object o = objs[0]; return convertString(o); } else { Object[] newObjs = Object[objs.length]; for (int i=0;i&lt;objs.length;i++) { newObjs[i] = convertString(objs[i]); } return (Object) newObjs; } } private Object convertString(String value, Class&lt;&gt; desiredClass) { if (Number.class.isAssignableFrom(desiredClass)) { Double d = null; try { d = Double.parseDouble(value) } catch (NumberFormatException e) { // Not of the desired type, do whatever you want } if (Double.class.isAssignableFrom(desiredClass)) { return d; } else if (Long.class.isAssignableFrom(desiredClass)) { return d.longValue(); } else if (Integer.class.isAssignableFrom(desiredClass)) { return d.intValue(); } // and so on... } else if (Boolean.class.isAssignableFrom(desiredClass)) { try { return Boolean.valueOf(value); } catch (NullPointerException e) { // Not of the desired type, do whatever you want } } else { return value; // or whatever your default type is } } </code></pre>
 

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