简体   繁体   English

Spring验证框架多个模型属性错误消息未显示在JSP上

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

I am trying to validate 2 model attributes on one action using spring validation framework. 我正在尝试使用spring验证框架来验证2个模型属性。 The purpose is to validate the lookUpbean (search criterion) on click of Search button and then to validate the resultant bean also ie memberShipbean once we get it from the services so that we can show warnings to the user if some fields are empty in the resultant bean. 目的是在单击“搜索”按钮时验证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);

}

Now what is happening is first validation is working fine however during second validation the error messages are not shown on the resultant jsp , however the errors are reported till this controller layer in the “error” binding results. 现在发生的事情是第一次验证工作正常,但是在第二次验证期间,错误消息未显示在生成的jsp上,但是直到“错误”绑定结果中的此控制器层都报告了错误。

Also in the validation layer 同样在验证层

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

This method always returns validation errors even if the field is not empty. 即使该字段不为空,此方法也始终返回验证错误。

First question is can we have multiple model attributes in one action. 第一个问题是我们可以一次执行多个模型属性。 I read it somewhere on internet but could not find any implementation of the same. 我在互联网上的某个地方阅读了该书,但找不到相同的任何实现。 Please help me to resolve this issue. 请帮助我解决此问题。 Also let me know if you can think of any other working solution for this problem but I would like to use only the spring framework for the both the validations as that helps to maintain the existing design of the application. 还请让我知道您是否可以想到其他解决此问题的解决方案,但是我只想对这两个验证使用spring框架,因为这有助于维护应用程序的现有设计。

I do not think you have bind multiple Model Attributes to the same form, which I believe is what you are asking. 我认为您没有将多个模型属性绑定到同一表格,我相信这是您要的内容。 I am not even sure what that request would look like, from an HTTP stand point, as I don't believe Spring would have a way to detangle all the bean's properties, especially if the names overlap. 从HTTP的角度来看,我什至不确定该请求的外观,因为我不认为Spring不会提供一种方法来处理所有bean的属性,尤其是在名称重叠的情况下。 What you may want to consider doing is wrapping your LookupPageBean and MembershipPageBean into one "form bean". 您可能要考虑做的是将LookupPageBean和MembershipPageBean包装到一个“窗体bean”中。

As for the error messages, you may want to take a look at the spring:bind tag. 至于错误消息,您可能想看看spring:bind标签。 It may do what you are needed to get the binding errors from the second Model Attribute. 它可能会做您需要从第二个“模型属性”中获取绑定错误的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM