7

命令驗證錯誤我無法呈現從我的命令對象錯誤。它很好地完成了這項工作,但是我的.gsp視圖不會導致我提出的錯誤。呈現跨越重定向

這裏是我的控制器操作:

def handleModifyProfile2 = { CreditProviderModificationCommand cpmc -> // bind params to the command object 
    if (cpmc.hasErrors()) { 
     flash.message = "Error modifying your profile:" 
     redirect(action: "modifyProfile", params: [creditProvider : cpmc]) 
    } ... 

這裏是我嘗試呈現在我.gsp視圖中的錯誤:

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

我怎樣才能在要顯示的錯誤視圖?

回答

9

無法跨一個重定向使用params發送命令。你有幾個選擇:

  • 處於錯誤狀態,而不是redirect() ING render()

    if(cpmc.hasErrors()) { 
        render(view: 'profile', model: [creditProvider: cpmc]) 
    } 
    

    這是你在做什麼是最常見的成語。

  • 在會話中存儲的命令跨越重定向堅持它:

    if(cpmc.hasErrors()) { 
        session.cpmc = cpmc 
        redirect(...) 
    } 
    
    // and in your action 
    def cpmc = session.cpmc ?: null 
    render(view: 'profile', model: [creditProvider: cpmc]) 
    

    此選項是有點問題的。如果做得不正確,你可能會污染會議,並讓對象留下來,佔用記憶。但是,如果正確完成,它可以是一個體面的方式來實現重定向後獲取。

+0

啊! 您已經結束了3個小時的漫遊。 非常感謝! –

+0

當然,沒問題。 –

+1

謝謝,我發現你的答案也有幫助。順便說一句,我想使用閃光代替會議直接會防止會話污染? –