2011-03-10 26 views
2

我正在寫一個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和相關代碼之間建立鏈接。

希望我在這裏已經提供了足夠的信息,但如果有更多的要求,那麼請不要猶豫,問。

非常感謝,

埃德

回答

1

在嘗試調試類後,我感到非常尷尬,我發現它並沒有被調用。這個發現讓我意識到我沒有在JSP中包含Spring taglib命名空間聲明,包括解決了這個問題。

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 

罵我自己缺少這個現在很明顯的錯誤,有點困惑,爲什麼在JSP(和我的IDE)並沒有抱怨缺少的taglib聲明。我認爲一個丟失的taglib聲明通常會在執行該標記時導致RuntimeException,但似乎並非如此(我希望它能爲我節省好幾個小時的調試時間!)

無論如何,問題解決了。

@axtavt - 感謝您的幫助!

1

errors變量只露出裏面<spring:hasBindErrors>標籤,所以,你應該做這樣的事情(請注意,它也取代<c:if>):

<spring:hasBindErrors name="xApiRequest"> 
<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> 
</spring:hasBindErrors> 
+1

謝謝。其實我已經試過了 - 當我這樣做的時候,JSP呈現JSON對象(好像在說_were_ BindErrors存在,但對'$ {errors.errorCount}'等的調用不會產生任何錯誤,就好像沒有錯誤數據存在,任何進一步的想法? – 2011-03-10 12:11:57

+0

可悲的是,它完全一樣的事情,我已經確定標籤類'org.springframework.web.servlet.tags.BindErrorsTag',所以我要看看我是否可以調試發生了什麼 – 2011-03-10 12:44:48

+0

@Edd:看起來很奇怪,類似的代碼適合我。 – axtavt 2011-03-10 12:57:57

0

天啊!我有同樣的問題,非常感謝你回答你自己的問題。

在我的案例 - 希望可以幫助其他人在那裏 - 一個簡單的JSP,調用一個方法,如:

@RequestMapping(method = RequestMethod.GET) 
protected Object loadRegistrationForm(HttpSession session, Model model) { 

這個JSP返回具有可以提交,並得到一個服務器POST方法的表單,所以我有以下捕獲從形式後返回的錯誤:

<spring:hasBindErrors name="customerAccountFormObj">  

<div class="alert alert-block pull-right alertmessage alert-warning alert-dismissible" role="alert" id="errorMsgHdr"> 
        <button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button> 

       <form:errors path="*" cssClass="errorList" element="div"/> 
</div> 

</spring:hasBindErrors> 

基本上是一個自舉警報消息顯示錯誤時,有binderrors,猜get調用會發生什麼?我得到一個空的警告框 - 12小時敲打我的頭後,我無意中發現你的問題,增加

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 

瞧!一切都很乾淨,沒有空的警報框,即GET調用沒有錯誤。這必須是JSP/Spring中的一個錯誤。如果它幫助任何人,我使用Spring mvc 3.0.3.RELEASE。