2010-10-07 63 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