问题
I have set a variable in my controller class in session as follows:
session.webURL = webURL
println "#####" + session.webURL`
Now, I want to use this value of webURL in my src/groovy class. So I wrote in my src/groovy BasicCrawler class:
println session.webURL
It shows me an error: Message: 'No such property: session for class: cmsprofiler.BasicCrawler' at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
Please tell me what I am doing wrong. Thanks a lot.
回答1:
You can't access the session in any arbitrary class. You can pass the value as an argument to a method in BasicCrawler if you like.
// grails-app/controllers/demo/DemoController.groovy
package demo
class DemoController {
def someAction() {
def crawler = new BasicCrawler()
crawler.someMethod(session.webURL)
}
}
// src/groovy/demo/BasicCrawler.groovy
package demo
class BasicCrawler {
def someMethod(String url) {
// ...
}
}
来源:https://stackoverflow.com/questions/24638637/passing-session-value-from-controller-to-src-groovy-class