Session 在grails导航api中使用会话对象
我正在使用grails平台核心的导航api。我试图在其中一个链接的标题(人名)中使用会话变量。根据“可见性和状态”部分中的文档:Session 在grails导航api中使用会话对象,session,grails,Session,Grails,我正在使用grails平台核心的导航api。我试图在其中一个链接的标题(人名)中使用会话变量。根据“可见性和状态”部分中的文档: The closures receive a delegate which resolves the following standard Grails properties: grailsApplication pageScope session request controllerName actionName flash params 这似乎表明我将在nav
The closures receive a delegate which resolves the following standard Grails properties:
grailsApplication
pageScope
session
request
controllerName
actionName
flash
params
这似乎表明我将在navigation.groovy中提供该会话。在my 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
如果我用“hello world”替换“session.id”,一切正常。根据需要创建闭包:
注意闭包在脚本中是如何“def”的,以使其可重用
并且可在DSL内访问
闭包接收一个委托,该委托解析以下标准
Grails属性:
- grailsApplication
- 寻呼机
- 会议
- 请求
- 控制器名称
- actionName
- 闪光参数
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
}
您是如何将全名添加到会话的?你确定它在那里吗?即使我尝试session.id或session.toString(),也会显示错误。会话变量不存在。运气不好。首先,我必须用括号声明索引(titleText:indexTitleName()),以获得有效的输出。在没有()的情况下,我得到了一个神秘的闭包名称,如AppNavigation$\u run\u closure1_closure3@1caa59e6. 绕过该问题后,如果我尝试返回session.id,错误消息是:异常评估java.util.ArrayList的属性“id”,原因:groovy.lang.MissingPropertyException:没有这样的属性:class:org.grails.plugin.platform.conventions.DSLBlockCommandOk的id我在我的原始帖子中修改了代码,添加了完整的navigation.groovy。如果存在session.id,程序将不会启动。另外,我还包括了“def isBF”闭包,因为它实际上可以正常工作,没有任何问题。您可以尝试从导航块中声明闭包吗?将您的代码与我的代码进行比较,我认为这就是区别。这次将def移到导航外部时出现了不同的错误:错误2013-10-18 12:56:55997[localhost-startStop-1]ERROR context.grailContextLoader-初始化应用程序时出错:无此类属性:类的会话:AppNavigation消息:无此类属性:类的会话:AppNavigation行|方法->>7 | AppNavigation$_run_closure1中的doCall