Results 1 to 2 of 2

Thread: PropertyEditorSupport setAsText not invoked

  1. #1
    Join Date
    Dec 2007
    Posts
    7

    Default PropertyEditorSupport setAsText not invoked

    Hi folks,
    I'm having an issue with PropertyEditorSupport. I've got 2 classes Photo and Category. Then I have a form, you can fill in name and select category from the selectbox.

    The jsp part of the form :
    PHP Code:
    <spring:bind path="photo.category">
        <
    label for="<c:out value="${status.expression}"/>"><fmt:message key="photoform.category.name"/></label>
        <
    select name="<c:out value="${status.expression}"/>">
              <
    c:forEach var="category" items="${categories}">  value="<c:out value="${category.id}"/>"><c:out value="${category.name} ${status.value}"/></option>
            </
    c:forEach>
        </
    select>    
    </
    spring:bind
    I have registered my CustomEditor :
    PHP Code:
    protected void initBinder(HttpServletRequest requestServletRequestDataBinder binder) {
            
    binder.registerCustomEditor(Category.class, new CategoryEditor(categoryDao));
        } 
    The property editor:
    PHP Code:
    public class CategoryEditor extends PropertyEditorSupport{
        protected final 
    Log logger LogFactory.getLog(getClass());
        private 
    CategoryDao categoryDao;

        public 
    CategoryEditor(CategoryDao categoryDao) {
            
    this.categoryDao categoryDao;
        }

        public 
    String getAsText() {
            
    Category cat = (CategorygetValue();
            
    logger.info("Converting to int byId: " +cat.getId());
            return 
    Integer.toString(cat.getId());
        }
        public 
    void setAsText(String[] text) {
            
    logger.info("in setAsText" text.toString());
            
    Category cat;
            
    cat=categoryDao.getCategoryById(Integer.valueOf(text[0]));
            
    logger.info("Setting category "+text +" name: " +cat.getName());
            
    setValue(cat.getId());
        }

    During the form setup - in formBackingObject I can see the call of getAsText, but when I submit the form setAsText is not called. It also genereates an exception:

    Field error in object 'photo' on field 'category': rejected value [[Ljava.lang.String;@1fdde41]; codes [typeMismatch.photo.category,typeMismatch.category, typeMismatch.org.power_soft.photobook.bus.Category ,typeMismatch]; arguments [org.springframework.context.support.DefaultMessage SourceResolvable: codes [photo.category,category]; arguments []; default message [category]]; default message [Failed to convert property value of type [java.lang.String[]] to required type [photobook.bus.Category] for property 'category'; nested exception is java.lang.IllegalArgumentException: 58,72]


    58 is the default value, 72 is the new one. I can see that String array is returned instead of string, I reckon that is the reason why it doesn't call setAsText. Unfortunately I have no clue how to get around this

    any ideas why it returns the array and/or how to deal with it?
    thanks Vitek

  2. #2
    Join Date
    Aug 2004
    Location
    Burgas, Bulgaria
    Posts
    37

    Default

    Code:
    public void setAsText(String[] text) { 
            logger.info("in setAsText" + text.toString()); 
            Category cat; 
            cat=categoryDao.getCategoryById(Integer.valueOf(text[0])); 
            logger.info("Setting category "+text +" name: " +cat.getName()); 
            setValue(cat.getId()); 
        }
    You have registered your property editor for Category.class, yet in your setValue you are setting cat.getId() ... probably a Long or BigInteger or whatever.

    Try setValue(cat);
    Last edited by khote; Dec 29th, 2007 at 11:27 PM. Reason: change quote to code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •