Reputation: 1017
I am just starting out trying to use Services in Grails in order to Streamline my application and I want to send a Service a boolean value, from a controller, as a parameter and have it process the data and either do something or just return but I keep getting errors and I am not sure why. Below is my code:
Controller Code:
def testing(){
def userObjects = springSecurityService.currentUser
GroupCheckService.isEnabled(userObjects.group.notenabled)
}
Service Code:
class GroupCheckService {
static transactional = true
def useable(boolean notenabled) {
if(notenabled == true){
render(view:'/locked')
}else{
return
}
}
}
Now the value of the "notenabled" item is a boolean and the code works if I have it inside the controller however I want to only have this is code written in one place so I can call it from multiple function and controllers. When I run this code above and try to load the 'testing' view I get this error:
Error Code:
| Error 2013-09-27 22:55:21,882 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - ClassCastException occurred when processing request: [GET] /my_app/feature/testing
com.tool.GroupCheckService cannot be cast to com.tool.GroupCheckService. Stacktrace follows:
Message: com.tool.GroupCheckService cannot be cast to com.tool.GroupCheckService
Can someone please tell me what I am doing wrong and why I cannot pass this boolean to the service?? Thanks in advance
********EDIT*********
I have now got the service being called correctly with the param however I had another issue with the fact that the render didn’t work, as i suspected this wouldn’t work due to the fact it is outside the controller I implemented the code changes below to fix the issue however this also didn’t work. There were no errors but when the group is locked and it goes inside the first IF the view is not rendered :S
import grails.gsp.PageRenderer
class GroupCheckService {
PageRenderer groovyPgeRenderer
static transactional = false
def isEnabled(boolean notenabled) {
if(notenabled == true){
println("INSIDE LOCKED IF")
groovyPgeRenderer.render(view: '/locked')
}else{
return
}
}
}
I appreciate any help or advise that can be given on this. Thanks
Upvotes: 0
Views: 548
Reputation: 290
No, you can't directly render view from service, since you don't have render method available in service classes. Although, there is way around this is to pass render as a closure from controller and call it in service.
Controller code
class MyController
{
def myService
def myAction()
{
myService.myFun(render,true)
}
}
Service Code
class MyService
{
def myFun(Closure render,boolean flag)
{
if(flag)
render.call view:"view1"
else
render.call view:"view2"
}
}
Upvotes: 1
Reputation: 418
Controller Code:
def groupCheckService
def testing(){
def userObjects = springSecurityService.currentUser
def enabled = groupCheckService.isEnabled(userObjects.group.notenabled)
enabled? render(view:'/unlocked'): render(view:'/locked')
}
Service Code:
class GroupCheckService {
Boolean isEnabled(Boolean notenabled) {
!notenabled
}
}
Upvotes: 0