Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject a simple map into a Grails controller

Tags:

config

grails

I'm trying to inject a Map into a Grails controller. I want to inject the same map into many controllers so would like to define it in resources.groovy.

I've looked on the web but can't find an example of creating a simple map.

In Spring MVC I've 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 doesn't seem to work in grails if I use the spring xml files.

Any tips appreciated.

like image 649
Matt Avatar asked Jan 01 '26 18:01

Matt


1 Answers

after further investigation, you can create the 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" 
                ] 
        }
    }
like image 105
Aaron Saunders Avatar answered Jan 06 '26 23:01

Aaron Saunders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!