3

Map を Grails コントローラーに挿入しようとしています。多くのコントローラに同じマップを注入したいので、resources.groovy で定義したいと思います。

Web を調べましたが、単純なマップを作成する例が見つかりません。

Spring MVC では、次のようなものを使用しました。

<util:map id="diplomaPermissions">
   <entry>
      <key>1</key>
      <value>Diploma_AQA_Building_And_Construction</value>
   </entry>
   <entry>
      <key>1</key>
      <value>Diploma_Edexcel_Building_And_Construction</value>
   </entry>
</util:map>

これを私のxmlヘッダーに入れます:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-2.5.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">

しかし、Spring xml ファイルを使用すると、これは grails では機能しないようです。

ヒントをいただければ幸いです。

4

2 に答える 2

9

さらに調査した後、「resources.groovy」でマップを作成できます

    beans = {
        diplomaPermissions(org.springframework.beans.factory.config.MapFactoryBean) {
        sourceMap = [
                  1:"Diploma_AQA_Building_And_Construction", 
                  2:"Diploma_Edexcel_Building_And_Construction" 
                ] 
        }
    }
于 2009-10-20T00:06:44.797 に答える
2

あなたが期待している春のバージョンに関連している可能性があると思います。マップの作成に古いスタイルを使用すると、すべて正常に動作します。

spring 構成ディレクトリの resources.xml でこれを試してください

<bean id="testMap" 
     class="org.springframework.beans.factory.config.MapFactoryBean">
  <property name="sourceMap">
      <map>
        <entry key="key1" value="value1"/>
        <entry key="key2" value="value2"/>
      </map>
  </property>
</bean>

そしてこれはあなたのコントローラーで

class DisplayMapController {
    def  testMap

    def index = {  
        render (contentType: "text/plain") {
                    div(id:"myDiv") { p "$testMap" }
            }
    }
}
于 2009-10-19T20:14:13.487 に答える