java - 在 MultiActionController ModelAndView 方法中调用 valang validator ...可能吗?如何?

标签 java spring spring-mvc

这可能吗?我基本上是使用 MultiActionController 类制作 Spring CRUD Web 应用程序,并且希望验证我的表单。我之前使用过 SimpleUrlController,valang 工作得很好。

最佳答案

根据MultiActionControlller API

Consider direct usage of a ServletRequestDataBinder in your controller method, INSTEAD OF relying on a declared command argument. This allows for full control over the entire binder setup and usage, INCLUDING THE INVOCATION OF VALIDATORS and the SUBSEQUENT EVALUATION of binding/validation 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();

        ...
    }
}

问候,

关于java - 在 MultiActionController ModelAndView 方法中调用 valang validator ...可能吗?如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1288023/

相关文章:

java - 为什么这个基于数组的 Java 游戏不起作用?

java - 是否可以从 Gradle 依赖项修改 Android Studio 中库文件的源代码

Spring Security 自定义过滤器被基本的 http 安全覆盖

java - org.springframework.beans.factory.BeanCreationException : in Ecommerce project

java - Spring @PropertySource 刷新

java - 测试 Spring 的 @Async void-returning 方法

java - 在 Spring Controller 中使用 JSON Patch 时出现 HttpMessageNotReadableException

java - 更新我的spring版本会阻止gradle解决spring-boot-starter

java - SpringMVC请求参数的值

java - 如何在struts2中创建自定义错误页面?