0

我正在使用来自 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' 一切都很好。

4

1 回答 1

0

根据您需要创建闭包的文档:

请注意如何在脚本中“定义”闭包以使它们在 DSL 中可重用和可访问

闭包接收一个代表,它解析以下标准 Grails 属性:

  • grailsApplication
  • 页面范围
  • 会议
  • 要求
  • 控制器名称
  • 动作名称
  • 闪存参数

所以你需要类似的东西:

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
}
于 2013-10-17T12:02:12.260 回答