Announcement Announcement Module
Collapse
No announcement yet.
Problem with custom property editors and Form Select TLD Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with custom property editors and Form Select TLD

    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:
    Code:
    public class ItemCategory  {
        private int itemCategoryID;
        private String categoryName;
    
        public ItemCategory() {
        }
    My Custom Property Editor for the previous object:
    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 Controller InitBinder method:
    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)));
        }
    My Form:
    Code:
    <frm:form modelAttribute="requestCategory">
    	<fieldset>
    		<legend>Item Category&nbsp;</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>
    And finally the modelAttribute object used in the previous form that contains the nested ItemCategory object:
    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

  • #2
    I ran into an issue similar to this last week. My object wasn't being selected in an option list when the form was being re-rendered.

    Simple fix: In your ItemCategory object, override isEquals() to check the id's of your objects. That is what the option tag uses to figure out if it should select the option or not.

    Hope this helps

    Comment


    • #3
      Well, i should have tough to redefine equals method but i solved my problem replacing my int primitive by Integer object

      I tried it before and it wasnt working, probably i didnt clean and build my project properly because now its working like it should...

      thanks a lot, anyway!

      Oliver-T

      Comment

      Working...
      X