Hello everyone,
I am basically new to Spring, I started using Spring Framework like 1 month ago.
I use Spring 2.5.6 with webmvc module, annotations and tld.
I have a problem Custom Binding a POJO with its ID in a Select Html tag.
When I fill my form and Add the object in a list everything works, but when I try to put back the added objet in the form,
the select tag does not bind correctly.
I think there is a bug in the SelectedValueCompartor Class of
Spring that is unable tho convert integer in Spring before comparing them.
Or i simply missed something???
Let me put you in context:
Here is my pojo that I want to custom bind:
My Custom Property Editor for the previous object:Code:public class ItemCategory { private int itemCategoryID; private String categoryName; public ItemCategory() { }
My Controller InitBinder method:Code:public class ItemCategoryEditor extends ClassEditor { private List<ItemCategory> list; public ItemCategoryEditor(List<ItemCategory> list) { this.list = list; } @Override public void setAsText(String text) throws IllegalArgumentException { LogFactory.getLog(ItemCategoryEditor.class).debug("text: " + text); if (StringUtils.hasText(text)) { for (ItemCategory ic : list) { if (text.equals(ic.getItemCategoryID())) { setValue(ic); break; } } } else { setValue(null); } } @Override public String getAsText() { LogFactory.getLog(ItemCategoryEditor.class).debug("value: " + getValue()); if (getValue() != null) { return ((ItemCategory)getValue()).getItemCategoryID(); } return super.getAsText(); } }
My Form:Code:@InitBinder("requestCategory") public void initRequestCategoryBinder(WebDataBinder binder, WebRequest wr) { LogFactory.getLog(RequestController.class).debug("Binding!"); binder.registerCustomEditor(ItemCategory.class, "itemCategory", new ItemCategoryEditor((List)wr.getAttribute("itemCategoryList", wr.SCOPE_SESSION))); }
And finally the modelAttribute object used in the previous form that contains the nested ItemCategory object:Code:<frm:form modelAttribute="requestCategory"> <fieldset> <legend>Item Category </legend> <br> <table> <tr> <td class="formLabel">Item Category</td> <td> <frm:select path="itemCategory"> <option value="0" class="selection">Choose</option> <frm:options items="${itemCategoryList}" itemLabel="categoryName" itemValue="itemCategoryID"/> </frm:select> <frm:errors path="itemCategory" cssClass="error"/> </td> </tr>
Code:public class RequestCategory implements GridTagSupport { private int requestCategoryID; private Request request; private ItemCategory itemCategory; private String userID; private Calendar requestFrom; private Calendar requestTo; private Rental rental; public RequestCategory() { }
I have tested a Integer object to replace my int for itemCategoryID to see if the select tag bind the id correctly and it did not worked.
Also Tested a String object and it worked!
I dont really want to use this work arround, i think this feature should be fixed to work with primitive, IMO
If you have any other solutions or ideas, that would be appreciate.
Thanks in advance
Oliver



Reply With Quote
