我需要返回“渲染"吗?在 Grails 中? [英] Do I need to return "render" in Grails?
本文介绍了我需要返回“渲染"吗?在 Grails 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有必要退货吗?假设它正在执行操作.
Is that return necessary? Let's say it's in the middle of an action.
render(contentType:'text/json', text: ['success': true] as JSON)
return
推荐答案
如果不返回,render
之后的任何代码也会被执行,这往往不是你想要的,例如
If you don't return, any code after render
will also be executed, which is often not what you want, e.g.
def someAction = {
if (someCondition) {
render view: 'success'
// if we don't return execution would fall through to the code below
return
}
log.error 'something went wrong'
render view: 'error'
}
当然,如果你改用这个样式,就不用返回了
Of course, if you use this style instead, there's no need to return
def someAction = {
if (someCondition) {
render view: 'success'
} else {
log.error 'something went wrong'
render view: 'error'
}
}
如果一个动作只有一个退出点,在render
If an action only has one exit point, there's no need to return after render
def someAction = {
render view: 'success'
}
请记住,如果不返回,render
之后的代码将被执行.
Just remember that the code after render
will be executed if you don't return.
这篇关于我需要返回“渲染"吗?在 Grails 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文