Call the boulder validator in the MultiActionController ModelAndView method ... maybe? as?

Is it possible? I basically create a Spring CRUD web application using the MultiActionController class and want my forms to be validated. I used SimpleUrlController before, and valang works fine.

+3
source share
1 answer

According to API MultiActionControlller

Consider the direct use of ServletRequestDataBinder in your controller method, INSTEAD OF relying on the declared argument of the command. This allows full control over the entire setup and use of the binder, INCLUDING VALIDATOR INVALIDATION AND THE FOLLOWING ASSESSMENT of binding / verification errors.

,

public class AddMultiActionController extends MultiActionController {

    public AddMultiActionController() {
        // Set up Valang according to your business requirement
        // You can use xml settings instead
        setValidators(new Validator [] {new CommandValidator(), new AnotherCommandValidator()});
    }

    public ModelAndView addCommand(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Command command = new Command();

        // createBinder is a MultiActionController method
        ServletRequestDataBinder binder = createBinder(request, command);
        // Sets up any custom editor if necessary
        binder.registerCustomEditor(...);

        binder.bind(command);

        return (!(binder.getErrors().hasErrors())) ? new ModelAndView("success") : new ModelAndView("failure");
    }

    // similar approach as shown above
    public ModelAndView addAnotherCommand(HttpServletRequest request, HttpServletResponse response) throws Exception {
        AnotherCommand anotherCommand = new AnotherCommand();

        ...
    }
}

,

0

Source: https://habr.com/ru/post/1715330/


All Articles