Hey guys,
This is what I've got so far.
I have a JSP that I use both for inserting a new record and editing a existing one. It looks like this:
My command class, Category, is as follows:Code:<body> <form:form commandName="category"> <table> <tr> <td>Description:</td> <td> <form:input path="description"/><form:errors path="description"/> <form:select path="parentCategory"> <option value="">Select...</option> <form:options items="${parentCategoryList}" itemValue="id" itemLabel="description"/> </form:select> </td> <td></td> </tr> <tr> <td colspan="3"> <input type="submit" value="Save Changes"/> <input type="button" value="Cancel" onclick="cancel();"/> </td> </tr> </table> </form:form> </body>
In my controller, I have written a custom property editor to handle this command:Code:public class Cateogry { private Long id; private String description; private Category parentCategory; ... }
The problem is that, the way it is, I can insert a record, save the association with its parentCategory and so on but, when I retrieve the record from the database in my formBackinObject() to display in the view, the parentCategory property, which is a drop-down, does not come with the actual value selected. It always come with the "Select..." option selected.Code:@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor(Category.class, "parentCategory", new PropertyEditorSupport() { @Override public String getAsText() { if (this.getValue() != null && this.getValue() instanceof Category) { return ((Category)this.getValue()).getId().toString(); } return this.getValue() == null ? "" : this.getValue().toString(); } @Override public void setAsText(String text) throws IllegalArgumentException { if (text != null && !text.equals("")) { setValue(categoryService.get(new Long(text))); } else { setValue(null); } } }); }
I have already verified that the parentCategory is being correctly populated before returning the view, and it is.
Any thoughts?
Thanks!


Reply With Quote

