Grails - previous redirect call (..) is already redirected

In the Grails application, sometimes seeing "Unable to transfer redirection" in the logs:

2011-04-27 12: 18: 40,469 [TP-Processor13] ERROR GrailsExceptionResolver - cannot issue a redirect (..) here. The previous redirect call (..) has already redirected the response. org.codehaus.groovy.grails.web.servlet.mvc.exceptions.CannotRedirectException: Unable to redirect (..) here. The previous redirect call (..) has already redirected the response. at com.coach.LoginController $ _closure2.doCall (LoginController.groovy: 90) ...

Not sure how to track this. Any ideas or suggestions?

Todd

+6
source share
3 answers

Check input controller; It looks like you are not returning from the action after the redirect. For instance:.

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

Although this question was answered, I decided that I shared my experience for future trawlers. Hope this helps.

This happened to me because after the redirect, I did not execute return :

  if (test) { flash.message = "Error message." redirect(action: "list") } switch ( params.test ) { case "value": redirect(action: "value", id: callInstance.id, version: callInstance.version) 

After the redirect, Grails will continue to move if there is no return . In my case, he got into switch and moved on to the second redirect, in which an error occurred. The code should look like this:

  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 

This code was anonymous, of course;)

EDIT

Hey Jize. I just realized that this is Sachin's answer: / Well, I will leave this as an additional example.

+3
source

You can redirect only once. If you go from the controller method A → B → C, B really should be a service method, which then passes the result to the controller method C, and not the controller method.

 class TemplateController { def templateService def A() { def results = templateService.B(params.input) redirect action: 'C', params: ['results': results] } def C() { return params.results } } 
+1
source

Source: https://habr.com/ru/post/886812/


All Articles