Java 在MultiActionController ModelAndView方法中调用valang验证程序。。。可能的怎样?

Java 在MultiActionController ModelAndView方法中调用valang验证程序。。。可能的怎样?,java,spring,spring-mvc,Java,Spring,Spring Mvc,这可能吗?我基本上是使用MultiActionController类制作一个SpringCRUD web应用程序,希望我的表单得到验证。我以前使用过SimpleUrlController,valang工作得非常好。根据API 考虑在控制器方法中直接使用ServletRequestDataBinder,而不是依赖于声明的命令参数。这允许完全控制整个活页夹的设置和使用,包括调用验证器和随后评估绑定/验证错误 所以你可以用 public class AddMultiActionController e

这可能吗?我基本上是使用MultiActionController类制作一个SpringCRUD web应用程序,希望我的表单得到验证。我以前使用过SimpleUrlController,valang工作得非常好。

根据API

考虑在控制器方法中直接使用ServletRequestDataBinder,而不是依赖于声明的命令参数。这允许完全控制整个活页夹的设置和使用,包括调用验证器和随后评估绑定/验证错误

所以你可以用

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();

        ...
    }
}
问候,