Announcement Announcement Module
Collapse
No announcement yet.
solved getting multiaction controller to work like a formcontroller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • solved getting multiaction controller to work like a formcontroller

    I've been working on getting request binding and validation working in a multi-action controller without having to use the form controllers. This way I can do all the usual CRUD work in a single class. (I know many of you have conceptual issues with this).

    - No need to change the views, they work exactly like views for form controllers so you can use the <form:form> and <spring:bind> tags with error tags and all that goodness

    - Imagine an Address class with members name,street,city,state,zip

    What we need to do is bind the incoming request to Address and validate it. If there's errors send the user back to the form view, otherwise process the Address object and send the user on to another view.

    Code:
    @RequestMapping(method=RequestMethod.POST)
    public String processAddress(HttpServletRequest request,ModelMap model) {
    
        // binding
        Object address = new Address();
        ServletRequestDataBinder binder = new ServletRequestDataBinder(address,"address");
        binder.bind(request);
        BindException errors = new BindException(binder.getBindingResult());
    
        // validation
        Validator validator = new AddressValidator(); /* probably should inject this */
        ValidationUtils.invokeValidator(validator, address, errors);
        Map bindMap = errors.getModel();
    
        if ( errors.hasErrors() ) {
            model.addAttribute("address",address);
            model.putAll(bindMap);
             return "formview";
        } else {
            
             // do some Address processing here like "ht.saveOrUpdate(address)"
    
             return "sucessView";
         }
    }
    Last edited by chasd00; Mar 17th, 2009, 03:14 PM. Reason: forum said i can't post links on the first post
Working...
X