这似乎违背了 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/