ken
ken

Reputation: 3745

Grails URLMappings

I have a plugin that relies on a custom url mapping. When i install the plugin i need to copy the content of the UrlMappings.groovy of the plugin and merge it with the one in the application where the plugin is installed

I would like however to register these url mappings directly into the grails framework without copying the content into the application itself. I don't want the user to change these mappings as they are core to how the plugins works.

Anyone can give me hints where to venture to achieve that.

Thank you

-ken

Upvotes: 2

Views: 1083

Answers (3)

Dmytro H.
Dmytro H.

Reputation: 116

Personally I use Java approach and inject mappings form plugin (I have only one plugin for that).

But generally my approach is following:

1.App

class UrlMappings {
    static mappings = DimaURLMappings.getMappings()
}

2.Plugin (called "Dima")

class DimaURLMappings {
    static def getMappings(){
       return {
           //Mappings here
        }
    }
}

Yes I need to add it manually, but on the other hand it's quite obvious where application gets that mappings, and there are no magic.

I use it in such way because I have few applications that use my plugin. If you have few plugins with mappings you will just need to merge them I think it is not hard. you could also return closure from plugin. But don't forget to change "delegate" in application.

Upvotes: 0

ken
ken

Reputation: 3745

Seems like i need to interface with UrlMappingsHolderFactoryBean directly to be able to do that. I was hoping that there might be an easier way to do that. The code below is taken from the UrlMappingPlugin itself, the only source that i found to help me solve my problem.

   if (application.isUrlMappingsClass(event.source)) {
        application.addArtefact(UrlMappingsArtefactHandler.TYPE, event.source)

        BeanBuilder beans = beans {
            grailsUrlMappingsHolderBean(UrlMappingsHolderFactoryBean) {
                grailsApplication = application
            }
        }

        ApplicationContext appCtx = event.ctx
        beans.registerBeans(appCtx)

        HotSwappableTargetSource ts = appCtx.getBean("urlMappingsTargetSource")
        ts.swap appCtx.getBean("grailsUrlMappingsHolderBean")
    }

Upvotes: 2

Aaron Saunders
Aaron Saunders

Reputation: 33345

Create another file in grails-app/config/ with a name ending in UrlMappings.groovy and put the customized mappings in there, for example myUrlMappings.groovy

Upvotes: 3

Related Questions