views:

40

answers:

1

I have used stipes on a project in the past, and it has a great TypeConverter library that can take request parameters and route them into JavaBeans. It can even handle maps and arrays, such that:

class A {
 private int num;
 private Map<String, Integer> map;
 private List<String> list;
 ... setters and getters ...
}

<input type='text' name='num'/>
<input type='text' name='map["a"]'/>
<input type='text' name='map["b"]'/>
<input type='text' name='list[0]'/>
<input type='text' name='list[1]'/>

I have considered just pulling that bit of code out of stripes, but it seems like this library must exist, I just don't know what it is called.

Reference info: I have access to Java6 JDK, spring, and this happens to be for a Jersey web service MessageBodyReader implementation, basically I'd like to write a generic BeanHandlerMessageBodyReader

+1  A: 

Check the Apache Commons BeanUtils framework.

Here, a code snippet extracted from the User Guide

HttpServletRequest request = ...;
MyBean bean = ...;
HashMap map = new HashMap();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()) {
  String name = (String) names.nextElement();
  map.put(name, request.getParameterValues(name));
}
BeanUtils.populate(bean, map);

It can process indexed and mapped properties, and also lets you define your own converters.

Tomas Narros