If I have a command object that stores a collection and I want the data binder to populate that collection from the servlet request, how should I name the parameters in my servlet request?
In Struts I can do field[index], but I can't see if the same is possible in Spring. I see BeanWrapper.getIndexedPropertyValue() , but no set() equivalent.
Many thanks,
Mike.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
FYI, I posted the same question earlier this week. Despite the fact that indexed properties aren't supported (yet), I did find a way to successfully populate an HTTP form with indexed properties.
In my application, I have a Header object that has a collection of Detail objects. The header parameters look normal, such as name="vendorName". For each one of my detail parameters, it looks like this: name="details[0].poNumber", where 0 is the index. I think this is a pretty standard way of doing this in Struts.
To get this working, first I created a subclass of ServletRequestDataBinder that looks like this:
public class PrefixedServletRequestDataBinder extends ServletRequestDataBinder {
public PrefixedServletRequestDataBinder(Object target, String name) {
super(target, name);
}
Header header = (Header) command;
for (int i = 0; i < header.getDetails().size(); i++) {
Object detail = header.getDetails().get(i);
String name = "details[" + i + "]";
PrefixedServletRequestDataBinder binder =
new PrefixedServletRequestDataBinder(detail, name);
binder.bind(req, name, ".");
}
}
This is working for my needs. So...
Mike, I would be curious to know if this suits your needs as well.
Juergen, do you see any "gotchas" with this approach. My only issue is that I will have to access my request parameters N + 1 times. Not sure if this is a problem right now.
Anyway, hope this is some help to somebody.
Ryan
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
If I have a command object that stores a collection and I want the data binder to populate that collection from the servlet request, how should I name the parameters in my servlet request?
In Struts I can do field[index], but I can't see if the same is possible in Spring. I see BeanWrapper.getIndexedPropertyValue() , but no set() equivalent.
Many thanks,
Mike.
Mike,
Sorry for the late response -- please see the thread at http://sourceforge.net/forum/forum.php?thread_id=958345&forum_id=250340
Juergen
Mike,
FYI, I posted the same question earlier this week. Despite the fact that indexed properties aren't supported (yet), I did find a way to successfully populate an HTTP form with indexed properties.
In my application, I have a Header object that has a collection of Detail objects. The header parameters look normal, such as name="vendorName". For each one of my detail parameters, it looks like this: name="details[0].poNumber", where 0 is the index. I think this is a pretty standard way of doing this in Struts.
To get this working, first I created a subclass of ServletRequestDataBinder that looks like this:
public class PrefixedServletRequestDataBinder extends ServletRequestDataBinder {
public PrefixedServletRequestDataBinder(Object target, String name) {
super(target, name);
}
public void bind(ServletRequest request, String prefix, String prefixSeparator) {
bind(new ServletRequestParameterPropertyValues(request, prefix, prefixSeparator));
}
}
Then, in my SimpleFormControll implementation that handles my form, I overrode onBindAndValidate:
protected void onBindAndValidate(HttpServletRequest req, Object command, BindException e)
throws ServletException {
Header header = (Header) command;
for (int i = 0; i < header.getDetails().size(); i++) {
Object detail = header.getDetails().get(i);
String name = "details[" + i + "]";
PrefixedServletRequestDataBinder binder =
new PrefixedServletRequestDataBinder(detail, name);
binder.bind(req, name, ".");
}
}
This is working for my needs. So...
Mike, I would be curious to know if this suits your needs as well.
Juergen, do you see any "gotchas" with this approach. My only issue is that I will have to access my request parameters N + 1 times. Not sure if this is a problem right now.
Anyway, hope this is some help to somebody.
Ryan