我正在寫一個Spring MVC 3基於Web的應用程序,使用JSP作爲視圖層。我在特定領域掙扎,我試圖在JSP中報告特定Model對象的BindingResult錯誤。這可能是最有相應的代碼解釋:無法獲得來自任何輸出<春天:hasBindErrors>標籤庫
這是我的春天控制器方法:
@RequestMapping(value = "/**", method = RequestMethod.GET)
public ModelAndView get(@ModelAttribute("xApiRequest") @Valid final XAPIRequest xApiRequest,
final BindingResult xapiBindingResult,
final HttpServletResponse response,
Model model) throws EntityNotFoundException {
String viewName = "/WEB-INF/views/get-single-entity.jsp";
/*
* Create a MAV passing in the original Model object which contains:
* 1: The 'xApiRequest' @ModelAttribute object.
* 2: The BindingResult for the 'xApiRequest' object.
*/
final ModelAndView mav = new ModelAndView(viewName, model.asMap());
final XAPIResponse<Resource> xApiResponse = buildXAPIResponse(false, 200, xApiRequest, null);
response.setStatus(200);
mav.addObject("xApiResponse", xApiResponse);
return mav;
}
當我執行這種方法,我可以看到以下內容:
- 將創建xApiRequest對象正確地將來自HttpServletRequest(I具有這是否一個單獨的方法)
- 的JSR-303的驗證,所造成的@Valid註釋,已經發生,並確定了2個驗證錯誤,這些被repre像我所期望的那樣感覺爲BindingResult對象。
- 的BindingResult對象存在於模型方法參數。
- xApiRequest和BindingResult對象已成功從模型方法參數傳輸到ModelAndView對象,該對象從該方法返回。
而且我可以證實,BindingResult的內容確實出現了正確識別xApiRequest對象作爲驗證錯誤的來源:
{[email protected], org.springframework.validation.BindingResult.xApiRequest=org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'xApiRequest' on field 'userId': rejected value [null]; codes [NotEmpty.xApiRequest.userId,NotEmpty.userId,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [xApiRequest.userId,userId]; arguments []; default message [userId]]; default message [may not be empty]
Field error in object 'xApiRequest' on field 'projectId': rejected value [null]; codes [NotEmpty.xApiRequest.projectId,NotEmpty.projectId,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [xApiRequest.projectId,projectId]; arguments []; default message [projectId]]; default message [may not be empty]}
而JSP是這樣的:
<%@ page contentType="application/json; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<%@ page session="false" %>
<spring:hasBindErrors name="xApiRequest">
</spring:hasBindErrors>
<c:if test="${errors}">
<json:object name="exceptions">
<json:property name="exceptionCount" value="${errors.errorCount}" />
<json:property name="globalExceptionCount" value="${errors.globalErrorCount}" />
<c:forEach var="error" items="${errors.allErrors}" varStatus="index">
<json:property name="${index}" value="${error.defaultMessage}" />
</c:forEach>
</json:object>
</c:if>
無論我做什麼,我似乎都無法接到呼叫以認識到xApiRequest model ob JECT有綁定錯誤,因此JSP輸出不包含例外錯誤的包含對象的細節:
{
w: false
s: 200
c: ""
r: {
o ~path: ""
}
}
有人能看到我在做什麼錯在這裏?沒有辦法,我可以在JSP處理過程中調試發生了什麼?我熱衷於調試Spring taglib,但我不太清楚如何在taglib和相關代碼之間建立鏈接。
希望我在這裏已經提供了足夠的信息,但如果有更多的要求,那麼請不要猶豫,問。
非常感謝,
埃德
謝謝。其實我已經試過了 - 當我這樣做的時候,JSP呈現JSON對象(好像在說_were_ BindErrors存在,但對'$ {errors.errorCount}'等的調用不會產生任何錯誤,就好像沒有錯誤數據存在,任何進一步的想法? – 2011-03-10 12:11:57
可悲的是,它完全一樣的事情,我已經確定標籤類'org.springframework.web.servlet.tags.BindErrorsTag',所以我要看看我是否可以調試發生了什麼 – 2011-03-10 12:44:48
@Edd:看起來很奇怪,類似的代碼適合我。 – axtavt 2011-03-10 12:57:57