2012-07-30 26 views
1

我明白,BindingResult是用於與表單有關的驗證錯誤。但是,怎麼樣經營錯誤,例如: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

感謝Jelies格式化JSP/HTML片段 – 2012-07-30 08:38:47

+0

沒有問題卡洛斯:) – jelies 2012-07-30 09:31:34

+0

http://stackoverflow.com/questions/3101148/how-to-add-error-上彈簧MVC-simpleformcontroller – Saurabh 2012-07-30 10:18:09

回答

6

您可以按照您的建議使用分離的對象,也可以將業務錯誤添加到Errors/BindingResult。就個人而言,我通常會將商業錯誤放入BindingResult中,因爲稍後更容易在JSP/View中顯示它們。我不知道是否有任何通用結構用於此目的。使用r.rejectValue("property", "error.object");,這就足夠了。或者,如果你願意,可以註冊全局錯誤調用r.reject("error.object");