grails - 我需要在Grails中返回 “render”吗?

标签 grails groovy

那需要退货吗?假设它处于 Action 中间。

render(contentType:'text/json', text: ['success': true] as JSON)    
return

最佳答案

如果您不返回,则render之后的任何代码也将被执行,这通常不是您想要的,例如

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'
}

当然,如果您改用这种样式,则无需返回
def someAction = {

  if (someCondition) {
    render view: 'success'

  } else {    
    log.error 'something went wrong'
    render view: 'error'
  }
}

如果一个 Action 只有一个退出点,则无需在render之后返回
def someAction = {
    render view: 'success'
}

只要记住,如果不返回,就会执行render之后的代码。

关于grails - 我需要在Grails中返回 “render”吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8714728/

相关文章:

unit-testing - 用多部分请求测试Grails Controller

c#-4.0 - 如何将这个 Groovy 函数转换为 C#?

unit-testing - Grails:如何在使用 Spock 进行单元测试时将值保存到 transient 字段?

Grails 为映射的 Url 提供 404

facebook - Grails - Spring Security/Social - Facebook 插件 setAccessToken 和 getAccessToken

eclipse - Gradle使用Eclipse Luna在GroovyShell中执行Groovy脚本

groovy - 在 groovy 中键入强制

Java StampedLock : what happens to writer while someone is reading

grails - 在grails gsp中调用标签不起作用

grails - Grails标记中的默认 body 行为