簡體   English   中英

Spring驗證框架多個模型屬性錯誤消息未顯示在JSP上

[英]Spring validation framework multiple model attributes error message not displayed on jsp

我正在嘗試使用spring驗證框架來驗證2個模型屬性。 目的是在單擊“搜索”按鈕時驗證lookUpbean(搜索條件),然后在從服務中獲取結果Bean(即memberShipbean)時也對其進行驗證,以便如果結果中某些字段為空,則可以向用戶顯示警告豆角,扁豆。

<form:form method="POST" modelAttribute="lookupPageBean" id="lookupForm" name="lookupForm" 
action="lookupMembership.htm">

   <td class="error">
  <form:errors path="membershipNumber" />   

  <form:input class="medium-textbox" id="membershipNumber" path="membershipNumber" />

  <button type="submit" class="Active-button-small">

  <fmt:message key="button.go" />
  </button>`


@RequestMapping(method = RequestMethod.POST, value = URLMappingConstant.MEMBERSHIP_LOOKUP)

public String viewMembership(ModelMap modelMap, HttpServletRequest request, HttpServletResponse   response,

@ModelAttribute(UIConstant.LOOKUP_PAGE_BEAN) LookupPageBean lookupPageBean, BindingResult result,

@ModelAttribute(UIConstant.MEMBERSHIP_BEAN) MembershipPageBean membershipPageBean, BindingResult error) throws WebMTracksException 
{
 membershipValidator.validate(lookupPageBean, result);
membershipValidator.validate(membershipPageBean, error);

}

現在發生的事情是第一次驗證工作正常,但是在第二次驗證期間,錯誤消息未顯示在生成的jsp上,但是直到“錯誤”綁定結果中的此控制器層都報告了錯誤。

同樣在驗證層

 ValidationUtils.rejectIfEmpty(errors, UIConstant.BUSINESSNAME,ValidationMSGConstants.BUSINESS_NAME)

即使該字段不為空,此方法也始終返回驗證錯誤。

第一個問題是我們可以一次執行多個模型屬性。 我在互聯網上的某個地方閱讀了該書,但找不到相同的任何實現。 請幫助我解決此問題。 還請讓我知道您是否可以想到其他解決此問題的解決方案,但是我只想對這兩個驗證使用spring框架,因為這有助於維護應用程序的現有設計。

我認為您沒有將多個模型屬性綁定到同一表格,我相信這是您要的內容。 從HTTP的角度來看,我什至不確定該請求的外觀,因為我不認為Spring不會提供一種方法來處理所有bean的屬性,尤其是在名稱重疊的情況下。 您可能要考慮做的是將LookupPageBean和MembershipPageBean包裝到一個“窗體bean”中。

至於錯誤消息,您可能想看看spring:bind標簽。 它可能會做您需要從第二個“模型屬性”中獲取綁定錯誤的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM