2010-10-07 65 views
3

我真的欣赏网络控制器的春天3 anoation驱动映射Spring 3 Web请求拦截器 - 如何获取BindingResult?

我有很多控制器有相似记号:

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

但我的问题是,我想写通过拦截器,将豪处理后的BindingResults - 如何从HttpRequest或HttpResponse获取它们?

作为intercpetor方法与相似签名

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

回答

1

所以从@Axtavt很大的帮助我来到conlusion,你可以得到到的postHandle方法从ModelAndView的绑定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); 
} 
8

控制器方法BindingResult的执行被存储为命名BindingResult.MODEL_KEY_PREFIX + <name of the model attribute>一个模型属性,以后模型属性合并到请求之后属性。因此,合并可以使用Hurda自己的答案,合并后使用前:

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

在文档中,我可以找到这样的信息吗? (我已经开始使用Spring 3.0了) – Hurda 2010-10-08 13:00:21

+1

@Hurda:在没有标准设施的情况下访问'BindingResult'(比如''标签)是一个相当先进的主题,所以在其javadoc中描述的模型中放置'BindingResult':http: //static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/validation/BindingResult.html – axtavt 2010-10-08 14:04:35

+0

所以我只是在请求属性中测试了这个和BindResult aint,但是在模型中。 OK,因为ModelAndView是void postHandle(HttpServletRequest请求,HttpServletResponse响应,Object handler,ModelAndView modelAndView)签名的一部分 – Hurda 2010-10-13 14:07:34