2013-10-16 23 views
0

我使用的是grails平臺核心的導航api。我試圖讓一個會話變量用於其中一個鏈接(一個人的名字)的標題中。根據文檔中的知名度和狀態:在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' 與'你好世界「一切都很好。

+0

您是如何將fullName添加到會話的?你確定它在嗎? – Vinny

+0

即使我嘗試session.id或session.toString(),也會顯示錯誤。會話變量只是不存在。 – spock99

回答

0

根據the docs您需要創建關閉:

Note how the Closures are "def"'d in the script to make them reusable and reachable within the DSL

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

  • 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 
} 
+0

沒有運氣。首先,我不得不用括號聲明index(titleText:indexTitleName())以獲得有效的輸出。沒有()我得到一個神祕的封閉名稱,像AppNavigation $ _run_closure1_closure3 @ 1caa59e6。解決後,如果我嘗試返回session.id錯誤是消息:異常評估屬性'id'爲java.util.ArrayList,原因:groovy.lang.MissingPropertyException:沒有這樣的屬性:ID爲類:org.grails.plugin .platform.conventions.DSLBlockCommand – spock99

+0

你能用新代碼更新你的問題嗎? –

+0

好的我修改了原始文章中的代碼以包含完整的navigation.groovy。如果session.id存在,程序將不會啓動。 – spock99