简体   繁体   中英

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. 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.

<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.

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.

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. What you may want to consider doing is wrapping your LookupPageBean and MembershipPageBean into one "form bean".

As for the error messages, you may want to take a look at the spring:bind tag. It may do what you are needed to get the binding errors from the second Model Attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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