[英]When does databinding and validation occur in spring mvc?
我最近瀏覽了Spring MVC文檔,並在DispatcherServlet下了解了以下請求處理流程
1)創建請求上下文
2)通過上下文啟動時准備的Handler映射找到Handler / Conroller
3)Exceute Interceptors preHandler方法(如果已配置)
4)執行Handler / Conroller方法
5)Exceute Interceptors postHandler方法(如果已配置)
6)處理異常(如果有)
7)渲染視圖
8)如果配置,則在完成之后執行攔截器
現在我有以下方法
@RequestMapping(value="/userHistory", method=RequestMethod.GET)
public @ResponseBody UserDetails getUserHistory(Model model, @valid UserDetail userDetail HttpServletRequest request, HttpServletResponse response) {
model.addAttribute("userDetail", new userDetail());
}
我的問題是在上述請求處理流程中的什么時間,DispatcherServlet將請求中的數據綁定到模型和userDetail對象?
類似地,dispacherservlet在什么時候觸發對帶有有效注釋的UserDetail對象的驗證?
更新:-說我已經在控制器方法下設置了customValidator,就像這樣。 現在,什么時候執行customValidation
和InitBinder
?
@InitBinder
private void initBinder(WebDataBinder binder) {
binder.setValidator(customValidator);
}
在步驟3和步驟4之間。數據綁定由參數解析器處理。 那些也負責驗證。
在您的示例中,您具有參數@valid UserDetail userDetail
。 Spring用於該參數的默認解析器是ServletModelAttributeMethodProcessor
。 它將傳入的表單數據綁定到UserDetail
實例,如果存在@Valid
或@Validated
批注,它還將驗證該對象。 這意味着驗證不是一個單獨的步驟,而是由參數解析器處理或根本不處理。
執行完所有參數解析程序后,將使用已解析的參數調用處理程序方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.