将会话值从控制器传递到src

将会话值从控制器传递到src

本文介绍了将会话值从控制器传递到src / groovy类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在会话中的控制器类中设置了一个变量,如下所示:

I have set a variable in my controller class in session as follows:

    session.webURL = webURL
    println "#####" + session.webURL`

现在,我想使用此值的webURL在我的src / groovy类。所以我在我的src / groovy BasicCrawler类中写道:

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

它显示我一个错误:消息:'没有这样的属性:类的会话:cmsprofiler.BasicCrawler'在org.codehaus.groovy.runtime.ScriptBytecodeAdapter。 unwrap(ScriptBytecodeAdapter.java:50)

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.

推荐答案

您无法在任何类别中访问会话。您可以将值作为参数传递给BasicCrawler中的方法。

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) {
        // ...
    }
}

这篇关于将会话值从控制器传递到src / groovy类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:15