New Grails controller doesn't seem to respond - grails

Grails 2.3.6 here. I went into myapp/grails-app/controllers and manually added a new WidgetController.groovy class:
class WidgetController {
def fizz() {
redirect(url: "http://google.com")
}
}
Then I run my Grails app locally, and when I go to http://localhost:8080/myapp/widget/fizz I just get my custom "page does not exist" error page. No errors in the logs.
Note: I did not use the grails create-controller Widget command; I just added a new file manually. What is going on here and what can I do to fix it?

Try to add a blank fizz.gsp page to your view/widget and it will work.

Related

#Transactional not applied to all controller actions

We notices after the update from Grails 3.1.11 to 3.2.0 that one action of a controller is no longer working:
#Transactional(readOnly = true)
class RoomPlanController {
...
def show(RoomPlan roomPlan) {
...
}
def getRooms(RoomPlan roomPlan) {
...
}
}
The problem is that when we call roomPlan/getRooms/1 roomPlan is null. If we call the show action with the same parameter roomPlan is set correct.
A call of getErrors() inside the controller gives us the following error message:
Could not obtain current Hibernate Session; nested exception is org.hibernate.HibernateException: No Session found for current thread
which has it's origin from grails.artefact.Controller.initializeCommandObject. After some more debugging I noticed a difference in the stacktrace between show and getRooms
Stacktrace of show:
show:100, RoomPlanController (at.byte_code.businessSuite.hotel)
$tt__show:-1, RoomPlanController (at.byte_code.businessSuite.hotel)
doCall:-1, RoomPlanController$_show_closure13 (at.byte_code.businessSuite.hotel)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
Stacktrace of getRooms:
getRooms:109, RoomPlanController (at.byte_code.businessSuite.hotel)
getRooms:-1, RoomPlanController (at.byte_code.businessSuite.hotel)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
The error message and the different stacktrace let us assume it has something to do with the database session/transaction and after adding #Transactional(readOnly = true) to the action everything work as expected and before the update to grails 3.2.0. If we remove the annotation and fails again.
We were not able see the issue in any other controller and were not able to reproduce it in a small test project. We already tried to rebuild the project, also on a completely new workstation we were not.
Did anybody else observed such an issue?
I don't think you would even need #Transactional(readOnly = true) in controller.
Grails controllers are by default readOnly. You can simply delete the annotation from the controller.
In contrast, Grails service class are transactional by default. If you need to call the save() method, it's more desirable to call that method in the service class.

grails webflow requested resource not available

I'm using grails 2.4.2 and the webflow-plugin :webflow:2.1.0-SNAPSHOT (the stable one 2.0.8.1 throws compile errors on 2.4).
The applicatin start without errors, so I hope everything is fine...
And now I'm trying to define a webflow...
In my controller I have the following code:
def newPartnerFlow() {
createPartner {
on("addAdresse").to("finish")
on("cancel").to("finish")
on("save").to("finish")
}
finish {
redirect controller: "stGsPartner", action: "index"
}
}
and to start the flow from the PartnerIndex-view, I have the following code:
<g:link class="btn btn-default" action="newPartner">Create WEB-Flow</g:link>
if I click now on the button in the index-page, I get the following error from tomcat:
message /MyApp/stGsPartner/newPartner
description The requested resource is not available.
Do I miss something?
Thanks in advance.
At least in older versions of webflow that I have used, the flow definitions had to be defined as closure variables rather than methods.
def newPartnerFlow = {
}

Using Groovy Library in Grails and getting 'No suitable ClassLoader found for grab'

I'm playing around with Grails/Groovy and have some straight Groovy code working that utilizes groovy-wslite. That code starts as such
send-request.groovy
#Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')
import wslite.soap.*
When I implement that into my Grails code and view the controller/action I get this
Error 500: Internal Server Error
URI: /FormProj/hello/trigger
Class: java.lang.RuntimeException
Message: No suitable ClassLoader found for grab
And here's the code in it's current state (I've tried a LOT of different things)
HelloController.groovy
package com.demo
import groovy.grape.Grape
class HelloController {
def index() { }
def sayHi() {
return [
greeting : "Hi there, ${ params.name }"
]
}
def trigger() {
Grape.grab(group:'com.github.groovy-wslite', module:'groovy-wslite', version:'1.1.0')
…
}
}
As I'm sure you notice I'm very green with Grails/Groovy and really all things Java. I do know there is a wslite plugin for Grails, but surely this can work too right?
Grails: 2.3.8
Groovy: 2.2.2
UPDATE
Based on Ian Robert's advice I have updated my BuildConfig file by adding this line to the dependencies block
compile 'com.github.groovy-wslite:groovy-wslite:1.1.0'
And updated my controller to look like this
HelloController.groovy
package ws.thejspot
import wslite.soap.*
class HelloController {
def index() { }
def sayHi() {
return [
greeting : "Hi there, ${ params.name }"
]
}
def trigger() {
def client = new SOAPClient('URL')
}
}
Unfortunately now the IDE, GGTS, shows an error in the controller 'unable to resolve class SOAPClient'
Rather than trying to download the dependencies with #Grab, you should use the standard Grails dependency mechanism - edit grails-app/conf/BuildConfig.groovy and look for the grails.project.dependency.resolution closure. Inside that, in the dependencies block you should add
compile 'com.github.groovy-wslite:groovy-wslite:1.1.0'
and remove anything Grape-related from the controller, leaving just the import wslite.soap.*
You will probably need to run
grails compile --refresh-dependencies
at least once to ensure that Grails picks up your change to BuildConfig - it deliberately doesn't do a full dependency resolve every time you compile, so as not to slow down the build too much, so you need to tell it to refresh when you know it needs to.

Grails GORM auto update issue

Updated post:
In a Controller if I do this:
def obj = new Test(name:"lol")
obj.save(flush:true)
obj.name = "lol2"
//a singleton service with nothing to do with obj
testService.dostuff()
/*
"obj" gets persisted to the database right here
even before the next println
*/
println "done"
Can anyone please explain me why is this happening with Grails 1.3.7 and not with Grails 2? What is the reason?
I know I could use discard() and basically restructure the code but I am interested in what and why is happening behind the scenes. Thanks!
Old post:
I have a test Grails application. I have one domain class test.Test:
package test
class Test {
String name
static constraints = {}
}
Also I have a service test.TestService:
package test
class TestService {
static scope = "singleton"
static transactional = true
def dostuff() {
println "test service was called"
}
}
And one controller test.TestController:
package test
class TestController {
def testService
def index = {
def obj = new Test(name:"lol")
obj.save(flush:true)
obj.name = "lol2"
testService.dostuff()
println "done"
}
}
So what I do:
Create a domain object
Change one of it's properties
Call a singleton service method
What I would expect:
Nothing gets persisted to the db unless I call obj.save()
What happens instead:
Right after the service call Grails will do an update query to the database.
I have tried the following configuration from this url: http://grails.1312388.n4.nabble.com/Turn-off-autosave-in-gorm-td1378113.html
hibernate.flush.mode="manual"
But it didn't help.
I have tested it with Grails 1.3.7, Grails 2.0.3 does not have this issue.
Could anyone please give me a bit more information on what is exactly going on? It seems like the current session has to be terminated because of the service call and because the object is dirty it is getting automatically persisted to the database after the service call. What I don't understand that even with the manual flush mode configuration in Hibernate does not help.
Thanks in advance!
I'm not sure what about that thread you linked to made you think it would work. They all said it wouldn't work, the ticket created has been closed as won't fix. The solution here is to use discard() as the thread stated.

Grails Blurb Plugin - Unable to resolve class Blurb

I'm working through the book Grails: A Quick-Start Guide and have come upon a problem. The book has me install the Blurb plugin, which seems to work, but states that we will be using it as if it were a domain class and using it a pre-existing controller. The code that I am to add to the controller looks like this
def blurb = Blurb.findByName("custom_${event.id}" )
if (!blurb){
blurb = new Blurb(name:"custom_${event.id}" , content:"" ).save()
}
When I do this I receive the same error in the IDE and the run output
'unable to resolve class Blurb' and I am directed specifically to this line blurb = new Blurb(name:"custom_${event.id}" , content:"" ).save()
Can anyone tell me what might be going wrong? I'm assuming the plugin is installed properly because if I try to access it's controller/action directly 'http://localhost:8080/TekDays/blurb/create' the plugin's provided view renders properly.
Thanks!
--
For reference I am using STS / Grails 1.3.7
Update 2011.05.12 7:45AM CST
I've attached a screenshot showing my project from the STS interface to show how my project is laid out in the event that it is package related as Burt indicated. The issue though is I'm not sure how do to the import statement so perhaps that screenshot will help.
Here is the current code in the Dashboard Controller.
package tekdays
class DashboardController {
...
}
I've tried adding the following lines per Burt's suggestion, but I obviously don't have it right
package tekdays
package my.package <--unexpected token: package
class DashboardController {
I tried changing out my with tekdays and default and both yield the same result.
Am I doing that wrong?
Thanks!
The Blurb class is in the default package, so if your controller is in a package you'll need to use a Groovy trick to access it:
package my.package
import Blurb as Blurb
class MyController {
def action = {
def blurb = Blurb.findByName("custom_${event.id}" )
if (!blurb) {
blurb = new Blurb(name:"custom_${event.id}" , content:"" ).save()
}
}
}

Resources