I am trying Spring MVC example. I have a testBean which has List type variable like
private List<HashMap<String, String>> books;
In my controller I have
@RequestMapping(value = "/booksList", method = RequestMethod.POST)
public String displayBooks(@ModelAttribute TestBean testBean, Model model, HttpSession session) {
// some code here
}
In my jsp page I have
<form:form action="booksList.html" method="post" modelAttribute="testBean">
<form:hidden path="books" />
<input type="submit" value="submit">
</form:form>
When I submit this form I am getting this error
Cannot convert value of type [
java.lang.String] to required type [java.util.HashMap] for property 'books[0]'.
How can I solve this error ? please help
Updated:
I have seen that I can do this way also.
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(List.class, "testBean", new CustomCollectionEditor(List.class) {
@Override
protected Object convertElement(Object element) {
TestBean testBean = new TestBean();
if (element != null) {
List<HashMap<String, String>> id = (List<HashMap<String, String>>) element;
testBean.setFilters(id);
}
return testBean;
}
});
}
But I don't understand the above method fully there may be some mistakes in what I have written. And I don't know how and from where I can call convertElement(Object element) method. i do understand initBinder(WebDataBinder binder) will call jsut before my controller method public String displayBooks(...). Even I don't know if its the right way of doing this.