在grails navigation api中使用session对象

时间:2013-10-16 23:01:46

标签: session grails

我正在使用grails平台核心的导航api。我正在尝试获取一个会话变量,用于其中一个链接的标题(一个人的名字)。根据可见性和状态部分中的文档:

The closures receive a delegate which resolves the following standard Grails properties:

grailsApplication
pageScope
session
request
controllerName
actionName
flash
params

这似乎表明我会在navigation.groovy中提供会话。在我的navigation.groovy中,我有一个菜单定义为:

import grails.util.GrailsWebUtil
import org.codehaus.groovy.grails.plugins.springsecurity.SecurityRequestHolder
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import org.springframework.orm.hibernate3.SessionFactoryUtils


navigation = {
    def isBF = { ->
        SpringSecurityUtils.ifAllGranted('ROLE_BF')
    }
    def indexTitleName = {  ->
        return session.id
    }
    app {
        home controller:'birthFamily', action:'contactInfo'
    }
    birthFamily {
        index(titleText:indexTitleName())
}

}

程序无法启动并生成此错误:

| Error 2013-10-18 08:16:57,286 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: Exception evaluating property 'id' for java.util.ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: id for class: org.grails.plugin.platform.conventions.DSLBlockCommand
Message: Exception evaluating property 'id' for java.util.ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: id for class: org.grails.plugin.platform.conventions.DSLBlockCommand
    Line | Method
->>   12 | doCall                           in NrfaNavigation$_run_closure1_closure3
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     28 | doCall                           in NrfaNavigation$_run_closure1_closure6
|     53 | __newBlock . . . . . . . . . . . in org.grails.plugin.platform.conventions.StandardDSLDelegate
|     66 | methodMissing                    in     ''
|     25 | doCall . . . . . . . . . . . . . in NrfaNavigation$_run_closure1
|     46 | build                            in org.grails.plugin.platform.conventions.StandardDSLBuilder
|     31 | evaluate . . . . . . . . . . . . in org.grails.plugin.platform.conventions.DSLEvaluator
|    280 | registerNavigation               in org.grails.plugin.platform.navigation.NavigationImpl

如果我将'session.id'替换为'hello world'一切都很好。

1 个答案:

答案 0 :(得分:0)

根据the docs,您需要创建闭包:

  

请注意如何在脚本中“关闭”闭包以使其可重用   并且可在DSL内访问

     

闭包收到一个代表,该代表解析了以下标准   Grails属性:

     
      
  • grailsApplication
  •   
  • pageScope
  •   
  • session
  •   
  • request
  •   
  • controllerName
  •   
  • actionName
  •   
  • flash params
  •   

所以你需要这样的东西:

def indexTitleName = {
  return session.fullName
}

navigation = {
  account {
    index(titleText:indexTitleName)
  }
}

修改

似乎只有在使用visible属性时它才会应用闭包。要获得会话,请使用导航类:

import org.codehaus.groovy.grails.web.util.WebUtils
def indexTitleName = {
  def webUtils = WebUtils.retrieveGrailsWebRequest()
  def session = webUtils.getCurrentRequest().getSession()
  return session.fullName
}