2012-02-15 108 views
3

我在Grails應用程序中使用Java實體類作爲域對象。這些類具有JSR 303驗證註釋(@Size,@NotEmpty等)。Grails:如何處理JSR 303 bean驗證

的Grails控制器和視圖做工精細與這些實體類,但是當我試圖挽救破碎的制約因素的實例(@NotEmpty字段爲空值),我得到一個錯誤頁面如下:

/webcall-account-manager-2/company/save 
Class 
javax.validation.ConstraintViolationException 
Message 
Validation failed for classes [com.rcslabs.webcall.server.model.Company] during persist time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=name, rootBeanClass=class com.rcslabs.webcall.server.model.Company, messageTemplate='{org.hibernate.validator.constraints.NotBlank.message}'} ] 

Around line 27 of grails-app/controllers/com/rcslabs/webcall/server/model/CompanyController.groovy 
24: def save() { 
25:  def companyInstance = new Company() 
26:  companyInstance.properties = params 
27:  if (!companyInstance.save(flush: true)) { 
28:   render(view: "create", model: [companyInstance: companyInstance]) 
29:   return 
30:  } 

Trace 
    Line | Method 
->> 251 | call  in org.grails.datastore.gorm.InstanceMethodInvokingClosure 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|  27 | save  in CompanyController.groovy 
| 1110 | runWorker in java.util.concurrent.ThreadPoolExecutor 
| 603 | run  in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 679 | run . . . in java.lang.Thread 

怎樣才能以正確的方式處理它(字段上方的消息框)?

回答

0

不是100%確定的,因爲我從未使用過JSR 303和Grails,但在保存之前可能會調用.validate。當你調用.save時,它正在運行驗證,並且似乎在拋出異常。因此,請嘗試手動執行驗證調用,然後您應該能夠以與在if塊中使用的方式類似的方式返回錯誤。

+1

companyInstance.validate()返回true – weekens 2012-02-16 07:09:21