Results 1 to 5 of 5

Thread: PropertyEditorSupport.setAsText() not firing

  1. #1

    Default PropertyEditorSupport.setAsText() not firing

    Long time spring framework user, new member as of today. I've searched around this forum and Google to an answer to my question without luck. I have an object (Enthusiast) that I am trying to populate using a custom property editor. I need to search the db with any of the given field values (email, facebook, twitter.. ECT) and return a result if there is one. The property editor works fine, but the problem revolves around the fields.

    Heres what I have inside the controller

    Code:
        @InitBinder("activityForm")
        public void initBinder(WebDataBinder binder, HttpServletRequest request) {
            binder.registerCustomEditor(Enthusiast.class, new EnthusiastEditor(request, new Enthusiast()));
        }
    Here is my form

    Code:
    <form action="/api/v2/activity" method="post">
        <input type="hidden" name="enthusiast.email" value="foobar@baz.com" />
        <input type="hidden" name="enthusiast.facebookId" value="90210" />
        <input type="submit" value="Submit" />
    </form>
    The problem is I don't have a hidden input with the name enthusiast. If I add that hidden input the setAsText method fires and everything works as expected. Is there a way around this? I need to get the enthusiast record if there is one before the data binding from the form happens so I am not married to using a custom property editor.

  2. #2

    Default

    Just kidding must of had a brain fart. Not sure if there's anything you can do to get around using the initBinder method but moving code from the property editor to a new @ModelAttribute init method worked fine.

    Code:
    @RequestMapping(value="/activity", method=RequestMethod.POST)
        public ModelAndView handlePost(@Valid @ModelAttribute("activityForm") ActivityForm form, Errors errors) {
            if (errors.hasErrors()) {
                return new ModelAndView(VIEW_NAME_ACTIVITY)
                    .addObject(ModelAttributes.ERRORS, errors)
                    .addObject(ModelAttributes.SUCCESS, false)
                ;
            }
            return new ModelAndView(VIEW_NAME_ACTIVITY)
                .addObject(ModelAttributes.SUCCESS, true)
            ;
        }
    
        @ModelAttribute("activityForm")
        public ActivityForm getActivityForm(HttpServletRequest request) throws Exception {
            ActivityForm form = new ActivityForm();
            //code from property editor here
            return form;
        }
    
    @InitBinder("activityForm")
        public void initBinder(WebDataBinder binder, HttpServletRequest request) {
            //other binders
        }

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    For your usecase you cannot use a PropertyEditor... A PropertyEditor / Converter is to be used if, for instance, you have an id which needs to be convertered into an object. It doesn't work for multiple fields and select one of those fields to query the database, for those things you need a @ModelAttribute method, as you already discovered .
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4

    Default

    Yea I knew that much not sure what I was thinking. The coded ended up getting a bit more complicated than expected so it was better to move into @ModelAttribute method. Thanks!

  5. #5
    Join Date
    Jul 2012
    Posts
    9

    Default

    Great one.l love this.

Posting Permissions

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