Embed a Simple Card in a Grails Controller

I am trying to insert a map into a Grails controller. I want to enter the same card in many controllers, so I would like to define it in .groovy resources.

I looked on the Internet, but I can not find an example of creating a simple map.

In Spring MVC, I used something like this:

<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>

With this in my xml header:

<?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">

But this does not work in grails if I use Spring xml files.

Any advice appreciated.

+3
source share
2 answers

after further research, you can create a map in "resources.groovy"

    beans = {
        diplomaPermissions(org.springframework.beans.factory.config.MapFactoryBean) {
        sourceMap = [
                  1:"Diploma_AQA_Building_And_Construction", 
                  2:"Diploma_Edexcel_Building_And_Construction" 
                ] 
        }
    }
+9
source

, spring, ; .

resources.xml spring

<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" }
            }
    }
}
+2

Source: https://habr.com/ru/post/1720471/


All Articles