0

私は grails プラットフォーム コアのナビゲーション API を使用しています。リンクの 1 つのタイトル (人の名前) で使用されるセッション変数を取得しようとしています。セクションの可視性とステータスのドキュメントによると:

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

ドキュメントによると、クロージャを作成する必要があります:

Closures がスクリプト内でどのように "def" され​​、DSL 内で再利用可能で到達可能になるかに注意してください。

クロージャーは、次の標準 Grails プロパティを解決するデリゲートを受け取ります。

  • grailsアプリケーション
  • pageScope
  • セッション
  • リクエスト
  • コントローラ名
  • アクション名
  • フラッシュパラメータ

したがって、次のようなものが必要です。

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 に答える