我知道BindingResult用于与表单相关的验证错误。但是,业务错误呢,例如:
public String bulkUpdate(@ModelAttribute form, BindingResult r) {
//do validation, extract selected issues to be bulk updated, etc.
//any form related, binding errors to be put in r
//this part
List<String> results = service.bulkUpdate(issues, newValues);
//where is it appropriate to put the results?
//from experience we just create a request attribute and put it there
request.setAttribute("bulkUpdateErrors", StringUtils.join(results, "<br>"))
//is there an similar generic structure like Errors?
}
在jsp中:
<div id='info'>
<c:if test="${not empty bulkUpdateErrors}">
<spring:message code="bulk.update.warning" /> ${bulkUpdateErrors}
</c:if>
</div>
是否存在类似的通用结构以解决业务错误?
答案 0 :(得分:6)
您可以按照建议使用分隔对象,也可以向Errors/BindingResult
添加业务错误。就个人而言,我通常会在BindingResult中添加业务错误,因为以后更容易在JSP / View中显示它们。我不知道为此目的是否有任何通用结构。
使用r.rejectValue("property", "error.object");
,就足够了。或者,如果需要,可以注册调用r.reject("error.object");