Grails: withFormat in the middle of action method
Is this a bug? I have the following block in the middle of my action method wrapped in the if stateme开发者_JS百科nt:
withFormat {
json{
render returnMap as JSON
return
}
}
The returnMap is rendered to the client just fine, but the method continues to execute as if return was never processed. What's up?
I am using grails 1.3.7.
To answer my own question: I found it has something to do with the withFormat block. If I remove it the return statement works just fine. When the withFormat is in place it seems that the return statement exits that block and continues the execution of the remaining method. Edit: Burt clarified below that it's the json{} closure that gets exited with the return statement (or without it I guess). If there are statements following that closure they will be executed.
The return exits the 'json' Closure, but not the whole method. It's like having methods with methods - you can only pop up one level. You'd need to set a flag inside the block and check it outside, something like
boolean renderedJson = false
...
withFormat {
json{
render returnMap as JSON
renderedJson = true
}
}
...
if (renderedJson) {
return
}
精彩评论