-
Aug 26th, 2008, 02:39 PM
#1
Spring MVC Binding problems
Hello,
I have a problem with data conversion between page and controller.
Step 1
Let's assume we have sample from your guide (paragraph 14.2.4.4)
Preferences {
String[] interests
}
and on the jsp I wrote form:checkbox path="interests"
It worked fine - the framework added or removed strings to or from the array
Step2
I used custom object instead of strings
Preferences {
Interest[] interests;
}
Interest{
String name
//constructor here
}
then I wrote InterestEditor and registered it in method annotated as InitBinder, you know...
It still worked fine
Step 3
I changed Interests array into Interests list
Preferences{
List interests
}
and it stopped working. After submitting the form, strings are being added to the list. But I expected Interest objects. What did I do wrong?
Thanks for advice,
mbartyzel
-
Aug 26th, 2008, 06:33 PM
#2
Can you post the relevant code?
-
Aug 27th, 2008, 10:45 AM
#3
The model objects are:
public class Employee {
private List interests = new ArrayList();
public List getInterests() {
return interests;
}
public void setInterests( List interests ) {
this.interests = interests;
}
}
public class Interest {
private String name;
public Interest( String name ) {
this.name = name;
}
public String toString() {
return name;
}
}
PropertyEditor is:
public class InterestEditor extends PropertyEditorSupport {
@Override
public void setAsText( String text ) throws IllegalArgumentException {
setValue( new Interest( text ) );
}
}
The controller part is:
@Controller
@RequestMapping( "/processForm.do" )
public class MyFormController2 {
@RequestMapping( method = RequestMethod.POST )
public String processForm( @ModelAttribute Employee employee, Errors errors ) {
System.out.println( "Iterest is class of" + employee.getInterests().get(0).getClass().getName( ) );
return "home";
}
@InitBinder
public void initBinder( WebDataBinder binder ) {
binder.registerCustomEditor( Interest.class, new InterestEditor() );
}
@RequestMapping( method = RequestMethod.GET )
public String setupForm( Model model ) {
model.addAttribute( new Employee() );
return "form";
}
}
The form.jsp code is:
<form:form modelAttribute="employee">
<form:input path="name"/>
dancing: <form:checkbox path="interests" value="dancing" />
traveling: <form:checkbox path="interests" value="travelling" />
swimming: <form:checkbox path="interests" value="swimming" />
<input type="submit" value="Submit" />
</form:form>
So now try to invoke /processForm.do URL, then chceck some checkboxes and submit. Notice what the controller will log on the sysout.
Thanks,
mb
-
Aug 28th, 2008, 07:23 AM
#4
anybody knows?
-
Aug 28th, 2008, 11:23 AM
#5
Why would you think this would work? Spring has no way of knowing that the List is supposed to contain Interest objects, so it's not going to bother invoking your custom property editor.
Contrast this to when you had an Interest[] array, where Spring has the runtime type information available to know which PropertyEditor to invoke.
Have a look at CustomCollectionEditor if you need to do something like what you're describing.
Peter Mularien | Blog
Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
SCJP 5, Oracle DBA
Any postings are my own opinion, and should not be attributed to my employer or clients.
-
Aug 28th, 2008, 01:50 PM
#6
-
Jan 10th, 2009, 04:28 AM
#7
Override equals() for proper data binding
Also, since it's a custom object, don't forget to override the equals() method! Otherwise if you submit to the same page, the checkboxes won't stay checked.
I couldn't figure this one out for hours (I'm slow and new to Spring MVC), and I'm so glad I stumbled across this thread, as it sparked the idea. You guys are awesome.
-
Jan 11th, 2009, 05:09 AM
#8
binding problem
Right...I had the exactly same problem when I tried to implement it in my dynamic website writing a business plan . After I read the posts in this forum I have succeeded to solve the problem.
Good luck there!
Best
Ben
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules