Passing session value from controller to src/groovy class

六月ゝ 毕业季﹏ 提交于 2020-01-06 15:52:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!