Spring 3 Web 请求拦截器 - 如何获取 BindingResult?

发布于 2024-09-26 17:51:57 字数 537 浏览 0 评论 0原文

我真的很欣赏 Spring 3 anoation 驱动的 Web 控制器映射

我有很多具有如下签名的控制器:

@RequestMapping(value = "solicitation/create",method = RequestMethod.POST)
public String handleSubmitForm(Model model, @ModelAttribute("solicitation") Solicitation  solicitation, BindingResult result) 

但我的问题是,我想编写一个拦截器,在处理后通过 BindingResults 进行处理 - 如何从 HttpRequest 或 HttpResponse 获取它们?

因为拦截器方法具有相似的签名

public boolean postHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

I realy appreciate Spring 3 anoation driven mapping of Web Controllers

I have a lot of Controllers with signatures like:

@RequestMapping(value = "solicitation/create",method = RequestMethod.POST)
public String handleSubmitForm(Model model, @ModelAttribute("solicitation") Solicitation  solicitation, BindingResult result) 

But my issue is, that I want to write an interceptor that would ho through BindingResults after processing - how do I get them from HttpRequest or HttpResponse?

as intercpetor methods are with alike signature

public boolean postHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云 2024-10-03 17:51:57

执行控制器方法 BindingResult 后,将其存储为名为 BindingResult.MODEL_KEY_PREFIX + 的模型属性,后续模型属性将合并到请求属性中。因此,在合并之前您可以使用 Hurda 自己的答案,合并之后使用:

request.getAttribute(BindingResult.MODEL_KEY_PREFIX + "solicitation")

After execution of controller method BindingResult is stored as a model attribute named BindingResult.MODEL_KEY_PREFIX + <name of the model attribute>, later model attributes are merged into request attributes. So, before merging you can use Hurda's own answer, after merging use:

request.getAttribute(BindingResult.MODEL_KEY_PREFIX + "solicitation")
生生漫 2024-10-03 17:51:57

因此,在 @Axtavt 的大力帮助下,我得出结论,您可以在 postHandle 方法中从 ModelAndView 获取 Bind reuslt :

void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
  String key = BindingResult.MODEL_KEY_PREFIX + "commandName";
  BindingResult br = (BindingResult) modelAndView.getModel().get(key);
}

So with big help from @Axtavt I came to conlusion, that you can get to Bind reuslt from ModelAndView in postHandle method:

void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
  String key = BindingResult.MODEL_KEY_PREFIX + "commandName";
  BindingResult br = (BindingResult) modelAndView.getModel().get(key);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文