2012-05-24 40 views
1

不確定我在這裏做錯了什麼。我希望這是微妙的。我似乎無法將錯誤傳遞給我的gsp頁面。我的控制器中有以下代碼:Grails控制器不會將錯誤傳遞到gsp頁面

def submit = { 
    if (params) { // if there are parameters 
     def sampleInstance = new Sample(params)// new sample 
     if (sample.validate()) { // try to validate 
      sample.save() 
      flash.message = "Successfully Entered Sample" 
      redirect (action: 'sample') 
     }else{ 
      flash.message = "Error Entering Sample" 
      sampleInstance.errors.each { 
       println it 
      } 
      redirect (action: 'sample', model:[sampleInstance:sampleInstance]) 
     } 
    } 
} 

我已驗證params不爲null。失敗的驗證會創建hasErrors(),它也已被驗證,並且代碼sample.errors.each {println it}正如我所期望的那樣通知我正確的Field錯誤。但是,我的重定向語法可能有什麼不對?因爲flash.message可以工作,但我無法訪問model:[sampleInstance:sampleInstance]地圖,並且不會顯示錯誤。

這裏是我的GSP代碼:

 <g:hasErrors> 
     <div class="errors"> 
     <g:renderErrors bean="${sampleInstance}" as="list" /> 
     </div> 

    </g:hasErrors> 

我的控制器被稱爲SubmitSampleController,動作被命名爲提交,以及GSP頁面被稱爲sample.gsp。

這可能是我的問題的答案:我有另一個稱爲示例的操作,也許我需要執行示例操作中的所有邏輯,而不是在提交操作中執行?還是有辦法將模型從一個動作傳遞給同一個控制器內的另一個動作?我有一種感覺,我的原始模型正在迷失。

def sample(){ 
    def now = new Date()// today's date 
    def today = com.Sample.findAllBySampleReceivedDateGreaterThanEquals(now.clearTime())// finds all samples submitted today 
    [checkDate:today, date: now] // passes a map of checkDate and todays date to the sample.gsp page 
} 

回答

1

在重定向,該模型被用作查詢參數這是不一樣呈現相同的,它不實際的前進,並把對象的請求範圍之內。如果出現錯誤,您需要執行的操作是渲染視圖並傳遞模型。然後你會得到所需的輸出。

+1

謝謝你的工作。爲了簡化事情,我最終將兩個操作合併爲一個,但知道我可以在同一個gsp頁面上使用多個操作是非常好的。乾杯! – Universitas

相關問題