我在每个保存对象的 Controller 方法中使用以下代码:
@RequestMapping(value="/user/saveX", method=RequestMethod.POST)
public String saveX(@Valid @ModelAttribute("myCommand") MyCommand myCommand) {
if(bindingResult.hasErrors()) {
return "/user/saveX";
}
...
...
}
我总是将用户返回到与
return "/user/saveX";
相同的页面并显示绑定(bind)错误,因此我在大多数 Controller 方法中重复此代码。我想发送这个回复
return "/user/saveX";
当 bindingResult 验证后出现错误时自动。有没有办法通过拦截器或过滤器来集中这种行为?
最佳答案
您应该寻找集成在 Spring 中的 AOP 编程范式。
http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#aop
关于java - Spring MVC : Automatically return back to the same page upon binding errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24575923/