3

我正在尝试将 Map 注入 Grails 控制器。我想将相同的地图注入到许多控制器中,所以想在 resources.groovy 中定义它。

我在网上查看过,但找不到创建简单地图的示例。

在 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 版本有关;使用旧样式创建地图时,一切正常。

在 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 回答