问题描述
我刚刚开始在Grails中使用服务,并且在尝试从服务内部呈现页面时遇到问题,我尝试了几个方法,如下所示,但没有运气:
服务呼叫1:
GroupCheckService.isEnabled(userObjects.group。 notenabled)
服务尝试1:
import grails.gsp.PageRenderer
$ b $ class GroupCheckService {
PageRenderer groovyPgeRenderer
static transactional = false
$ b $ def isEnabled(boolean notenabled){
if(notenabled == true){
groovyPgeRenderer.render(view:'/ locked')
} else {
return
}
}
}
服务呼叫2:
GroupCheckService.isEnabled( render,userObjects.group.notenabled)
服务尝试2:
class GroupCheckService {
PageRenderer groovyPgeRenderer
static transactional = false
$ b $ def isEnabled(Closure render,boolean notenabled){
if(notenabled == true){
render .call view:/ locked
} else {
return
}
}
}
$ b现在当我尝试方法1时,我没有收到错误,但是当IF满足时,页面不会呈现,方法2我得到以下错误:
no such property render
我真的很感谢一些建议或帮助如何实现这一点,在此先感谢:)
将服务用于业务逻辑和/或(事务性的)数据库操作s。
从关于使用
PageRenderer $ c
导入grails.gsp.PageRenderer
类GroupCheckService {
PageRenderer groovyPageRenderer
$ b def isEnabled(Closure render,boolean notenabled){
if(notenabled == true){
groovyPageRenderer.render view:/ locked
} else {
return
}
}
}
请注意,由于
PageRenderer
不在请求范围内,所以不能使用Sitemesh
布局。
最好的方法是重新设计你的应用程序,把渲染的东西留在控制器中。
I am just starting to use Services in Grails and I am having an issue when trying to render a page from inside a service, I have tried a few methods as shown below with no luck:
Service Call 1:
GroupCheckService.isEnabled(userObjects.group.notenabled)
Service Attempt 1:
import grails.gsp.PageRenderer class GroupCheckService { PageRenderer groovyPgeRenderer static transactional = false def isEnabled(boolean notenabled) { if(notenabled == true){ groovyPgeRenderer.render(view: '/locked') }else{ return } } }
Service Call 2:
GroupCheckService.isEnabled(render, userObjects.group.notenabled)
Service Attempt 2:
class GroupCheckService { PageRenderer groovyPgeRenderer static transactional = false def isEnabled(Closure render, boolean notenabled) { if(notenabled == true){ render.call view:"/locked" }else{ return } } }
Now when I try method 1 I don't get an error but the page is not rendered when the IF is satisfied, with method 2 I get the following error:
no such property render
I would really appreciate some advise or help on how to achieve this, thanks in advance :)
解决方案You shouldn't render views from service - this is a part for the controller.
Use the service for business logic and/or (transactional) database operations.
From grails docs:
If you realy want to render a view from service, mrhaki has blogged about using the
PageRenderer
from service.import grails.gsp.PageRenderer class GroupCheckService { PageRenderer groovyPageRenderer def isEnabled(Closure render, boolean notenabled) { if(notenabled == true){ groovyPageRenderer.render view: "/locked" }else{ return } } }
Notice that you can't use
Sitemesh
layouts due to thePageRenderer
works outside of the request scope.The best way is to redesign your application to leave the render stuff in the controller.
这篇关于Grails从服务中呈现视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!