grails - 使用拦截器时是否需要 render()?

标签 grails groovy

这似乎违背了 Grails 的“约定”性质。如果我没有明确调用 render()我认为方法,那么我的拦截器中的服务注入(inject)为空。

拦截器:

class GlobalParamsInterceptor {
def SysConfigService sysConfigService;

GlobalParamsInterceptor() {
    matchAll();
}
boolean before() {
    true
}

boolean after() {
    model.isApplicationOpen = sysConfigService.isApplicationOpen();

    true
}

void afterView() {
    // no-op
}
}

Controller :
class ConfigController {
static namespace = "coordinator";

def index() {
    render(view: "index");
}
}

如果我注释掉 render(view: "index")然后在我的 Controller 中sysConfigService在我的拦截器中是 null ,否则效果很好。有人可以解释为什么会这样吗?

编辑:
chalice 3.1.3

编辑#2
看来我弄错了,sysConfigService不为空...它的模型为空,所以我无法设置 isApplicationOpen属性(property)。尽管如此,我不认为我应该调用render()。方法来访问模型,因为我认为这是从框架继承的属性。

最佳答案

If I comment out render(view: "index") in my controller then sysConfigService in my Interceptor is null, otherwise it works great.



我不认为这是可能的。依赖注入(inject)到拦截器中发生并在任何请求进入应用程序之前完成。调用 render (或其他任何东西)在 Controller Action 中不会影响这一点。

此外,您还有 def SysConfigService sysConfigService . def有多余的。您可以删除它,编译器将生成相同的代码。 def仅当您删除 SysConfigService 时才有意义.

编辑:

发布后我意识到我没有解决标题中的问题:

Is render() is required when using Interceptors?



答案是不”。

In your example though, your calling render from your controller. My issue is that If I don't call render and rely on the "convention" of Grails to auto detect my "index.gsp" instead, that is when model in my interceptor is NULL.



参见 https://github.com/jeffbrown/raymond/commit/f354047158038f571630ab7e3e0416850bdde8a8 的提交.

当我向 /demo/report 发送请求时我在标准输出上看到以下输出:
Model: [name:Raymond]

该输出来自 https://github.com/jeffbrown/raymond/blob/f354047158038f571630ab7e3e0416850bdde8a8/grails-app/controllers/demo/FooInterceptor.groovy#L13 .

关于grails - 使用拦截器时是否需要 render()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35930576/

相关文章:

grails - 将外部JAR添加到Grails应用程序

java - Grails - Spring Security 无法与 Mysql 8 一起使用

ubuntu - 无法在 Tomcat 7 和 Ubuntu 中部署 grails 3 war - 404 错误

mongodb - 与托管Mongodb进行Grails集成测试

grails - 如何在Grails单元测试中模拟Ajax请求

gradle - 如何从Gradle中的子目录复制?

java - Groovy - 对象重用。

java - 如何在groovy中编写通用的数据库实用程序类?

从 Artifactory 删除大文件夹时,Jenkins 出现意外异常 java.nio.channels.ClosedByInterruptException

jenkins - 在 groovy 中以 10000 递增循环