(快速參考)

錯誤

目的

包含與此控制器相關聯的錯誤的 Spring Errors 介面的執行個體。

範例

class DemoController {

    static allowedMethods = [updatePerson: 'PUT']

    def generateReport(int size) {
        if(hasErrors()) {
            errors.allErrors.each {
                println it
            }
        }
    }

    def updatePerson(SomeDomainClass sdc) {
        // if params.id is found then a call
        // will have been made to SomeDomainClass.get(params.id)
        // to retrieve the instance from the database.
        // if no matching instance is found then
        // sdc will be null and the controller
        // will not have errors.  If params.id
        // is found and an exception occurs while
        // trying to retrieve the instance then
        // sdc will be null and an error will
        // be added to the controller
        if(sdc == null && hasErrors()) {
            errors.allErrors.each {
                println it
            }
        }
    }
}

說明

errors 屬性由 Grails 在處理要求時使用。當在處理動作參數時發生類型轉換,或是在嘗試從資料庫中擷取網域類別命令物件時引發例外狀況時,就會產生錯誤。

請參閱 hasErrors