Announcement Announcement Module
Collapse
No announcement yet.
PropertyEditorSupport.setAsText() not firing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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="[email protected]" />
        <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
    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
        }

    Comment


    • #3
      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 .

      Comment


      • #4
        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!

        Comment


        • #5
          Great one.l love this.

          Comment

          Working...
          X