在MultiActionController ModelAndView方法中调用valang验证器......可能吗?怎么样?

时间:2009-08-17 13:41:33

标签: java spring spring-mvc

这可能吗?我基本上使用MultiActionController类制作一个Spring CRUD Web应用程序,并希望我的表单得到验证。我之前使用过SimpleUrlController,valang完美运行。

1 个答案:

答案 0 :(得分:0)

根据MultiActionControlller API

  

考虑在控制器方法中直接使用ServletRequestDataBinder,INSTEAD OF依赖于声明的命令参数。这样可以完全控制整个活页夹的设置和使用,包括验证者的调用以及绑定/验证错误的后续评估。

所以你可以使用

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

        ...
    }
}

的问候,