gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:07:08 24 4
gpt4 key购买 nike

这可能吗?我基本上是使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com