2011-04-27 116 views
6

Grails中的應用,偶爾看到在日誌 「不能發出重定向」:Grails的 - 以前調用重定向(..)已經重定向

2011-04-27 12:18:40469 [TP-Processor13]錯誤GrailsExceptionResolver - 無法在此處發出重定向(..)。之前調用重定向(..)已經重定向了響應。 org.codehaus.groovy.grails.web.servlet.mvc.exceptions.CannotRedirectException:無法在此處發出重定向(..)。之前調用重定向(..)已經重定向了響應。 at com.coach.LoginController $ _closure2.doCall(LoginController.groovy:90) ...

不知道如何跟蹤它。任何想法或建議?

託德

+0

什麼是這裏:LoginController.groovy:90 ?? – Gregg 2011-04-27 18:50:35

回答

17

檢查登錄控制器;好像你沒有從重定向後的動作中返回。 例如

if (some condition) { 
    redirect() 
    return // should return from here to finish the action, otherwise the rest of the code will be executed 
} 
1

您只能重定向一次。如果從控制器方法A→B→C移動,B應該是一個服務方法,然後將結果傳遞給控制器​​方法C,而不是控制器方法。

class TemplateController { 
    def templateService 

    def A() { 
    def results = templateService.B(params.input) 

    redirect action: 'C', params: ['results': results] 
    } 

    def C() { 
    return params.results 
    } 
} 
3

雖然這個問題已經得到解答,但我想我會分享我對未來拖網漁船的經驗。希望能幫助到你。

這發生在我身上,因爲我沒有做一個return重定向後:

if (test) { 
     flash.message = "Error message." 
     redirect(action: "list") 
    } 

    switch (params.test) { 
     case "value": 
      redirect(action: "value", id: callInstance.id, version: callInstance.version) 

重定向後,Grails將繼續下去,如果沒有return。在我的情況下,它遇到了switch並繼續進行第二次重定向,這是錯誤發生的地方。該代碼應如下所示:

if (test) { 
     flash.message = "Error message." 
     redirect(action: "list") 
     return 
    } 

    switch (params.test) { 
     case "value": 
      redirect(action: "value", id: callInstance.id, version: callInstance.version) 
      return 

此代碼已被匿名的,當然;)

編輯

胡geeze。我剛剛意識到這是Sachin的答案:/好吧,我將把它作爲一個額外的例子。