我正在使用 Spring 3.1.1.RELEASE。我无法在我的 JSP 上显示错误消息。这是我在控制器中使用的方法...
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(final HttpServletRequest request,
final Organization organization,
final Model model,
final BindingResult result)
{
String nextPage = "organizations/add";
m_orgValidator.validate(organization, result);
if (!result.hasErrors())
{
final boolean isUpdate = !StringUtils.isEmpty(organization.getId());
// We need this clause to prevent detached entity errors.
if (StringUtils.isEmpty(organization.getId()))
{
organization.setId(null);
} // if
m_orgSvc.save(organization);
final String msgKey = isUpdate ? "org.updated.successfully" : "org.added.successfully";
final Object[] args = new Object[0];
request.setAttribute(STATUS,
MessageFormat.format(resourceBundle.getString(msgKey), args));
nextPage = "landing";
} else {
model.addAttribute(ORG_MODEL_NAME, organization);
} // if
return new ModelAndView(nextPage);
} // save
这是我的 JSP……</p>
<c:url var="action" value="/organizations/save" />
<form:form modelAttribute="org" method="post" action="${action}">
<form:hidden path="id" />
<fieldset>
<legend>Upload Fields</legend>
<p><form:errors path="*" cssClass="error" /></p>
<p>
<form:label for="name" path="name">Name *</form:label><br/>
<form:input path="name" type="text"/>
</p>
我已经通过调试验证了我的验证器正确设置了错误(result.hasErrors() = true),但是错误没有在我的页面上正确显示。有任何想法吗?如果有帮助,下面是我的验证器类。
@Component
public class OrgValidator implements Validator
{
@Autowired
private OrganizationService m_orgSvc;
…
public boolean supports(Class<?> clazz)
{
return Organization.class.isAssignableFrom(clazz);
}
public void validate(final Object target,
final Errors errors)
{
final Organization org = (Organization) target;
if (org != null && !StringUtils.isEmpty(org.getEodbId()))
{
final String eodbId = org.getEodbId();
final Organization foundOrg = m_orgSvc.findByEodbId(eodbId);
if ((org.getId() == null && foundOrg != null) ||
(org.getId() != null && foundOrg != null && !org.getId().equals(foundOrg.getId())))
{
errors.rejectValue("eodbId", "AlreadyExists.org.eodb.id");
} // if
} else if (org == null || org != null && StringUtils.isEmpty(org.getEodbId())) {
errors.rejectValue("eodbId", "NotExists.org.eodb.id");
} // if
if (org == null || org.getOrganizationType() == null)
{
errors.rejectValue("organizationType", "Invalid.org.type");
} // if
if (org == null || org.getCountry() == null)
{
errors.rejectValue("country", "Invalid.org.country");
} // if
if (org == null || org.getState() == null)
{
errors.rejectValue("state", "Invalid.org.state");
} // if
}
}