【问题标题】:Grails 3.3.8 - how to get session from a serviceGrails 3.3.8 - 如何从服务获取会话
【发布时间】:2019-06-10 13:09:47
【问题描述】:

好的,我有一个多模块项目,在一个插件中包含服务,以及多个使用它的网络“应用程序”。

用户和播放器服务有一个登录和注册方法,需要通过更新会话来“登录”播放器。

此帖:Grails get Session and Management in Service class

有一些解决方案,但似乎没有一个真正有效。 Webutils 在这些位置均不可用

import org.codehaus.groovy.grails.web.util.WebUtils
import org.grails.web.util.WebUtil
import org.grails.web.util.WebUtils

这也不起作用:

def session = getSession(true)

有什么建议吗?

这是来自插件的 build.gradle:

dependencies {
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.grails:grails-core"
    compile "org.grails.plugins:events"
    compile "org.grails.plugins:hibernate5"
    compile "org.hibernate:hibernate-core:5.1.5.Final"
    compile "org.grails.plugins:scaffolding"
    console "org.grails:grails-console"
    profile "org.grails.profiles:plugin"
    provided "org.grails:grails-plugin-services"
    provided "org.grails:grails-plugin-domain-class"
    runtime "org.glassfish.web:el-impl:2.1.2-b03"
    runtime "com.h2database:h2"
    runtime "org.apache.tomcat:tomcat-jdbc"
    testCompile "org.grails:grails-gorm-testing-support"
    testCompile "org.grails:grails-plugin-testing"
}

【问题讨论】:

  • “Webutils 在这些位置都不可用” - 但是,在 Grails 3.3.9 WebUtils does exist at org.grails.web.util.WebUtils 中,您不应该使用它。请参阅下面的答案。

标签: grails grails3


【解决方案1】:

有什么建议吗?

是的。

你可以这样做:

// grails-app/services/somepackage/SomeService.groovy
package somepackage

import grails.web.api.ServletAttributes

class SomeService implements ServletAttributes {

    void someMethod() {
        // you can access the session property directly from here
        println session

        // ...
    }
}

【讨论】:

  • 感谢您的回复。由于某些未知原因,它也无法解析 grails.web.api.ServletAttributes。它找到 grails.web.api,但没有找到 SeverAttributes。由于它也找不到 org.grails.web.util.WebUtils,这是项目中缺少的主要内容吗?服务在插件中定义。
  • grails.web.api.ServletAttributesgrails-web-common 中。
  • org.grails.web.util.WebUtils也是如此。
  • 我看到您在我回答问题后更改了 Grails 版本号。 Grails 3.3.8 的答案与 3.3.9 相同。
  • 如果您可以显示您的构建依赖项,这可能会有所帮助。使用grails create-plugin ... 创建的新插件应该具有必要的依赖关系。
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 2014-10-10
相关资源
最近更新 更多