开发者

Dynamic Grails Url Mapping config

开发者 https://www.devze.com 2023-01-26 05:56 出处:网络
How can I dynamically build a list of mappings - instead of: class UrlMappings { static mappings = { \"/helpdesk/user/$action?/$id?\" (controller=\"helpdeskuser\")

How can I dynamically build a list of mappings - instead of:

class UrlMappings {
static mappings = {
   "/helpdesk/user/$action?/$id?" (controller="helpdeskuser")
   "/helpdesk/group/$action?/$id?" (controller="helpdeskgroup")
   "/helpdesk/company/$action?/$id?" (controller="helpdeskcompany")
   "/helpdesk/account/$action?/$id?" (controller="helpdeskaccount")
   "/admin/company/$action?/$id?" (controller="admincompany")
   "/admin/account/$action?/$id?" (controller="adminaccount")
 }
}

something like this pseudo code:

class UrlMappings {
static mappings 开发者_如何学Go= {
   application.controllerClasses.each {
     if(it.name.startsWith('helpdesk'))
        "/helpdesk/${it.name}/$action?/$id?" (controller="${it.name}")
     if(it.name.startsWith('admin'))
        "/admin/${it.name}/$action?/$id?" (controller="${it.name}")
   }
 }
}

(I don't understand what the static mappings are - a hash map? free variables?)

What I am trying to achieve are mappings based on the controller type - e.g. helpdesk, admin or user controllers. Once I have set up the mappings I want to add security based on URLs but I don't want to map each controller individually:

grails.plugins.springsecurity.interceptUrlMap = [
   '/helpdesk/**':   ['ROLE_HELPDESK','ROLE_ADMIN'],
]


I've just done the following in my application:

import org.codehaus.groovy.grails.commons.ApplicationHolder

class UrlMappings {
  static mappings = {        
    for( controllerClass in ApplicationHolder.application.controllerClasses) {
      // Admin Controllers first
      if( controllerClass.name.startsWith("Admin")){
        // note... fixes the case so that AdminUserController maps to /admin/user
        "/admin/${controllerClass.name[5].toLowerCase() + controllerClass.name[6..-1]}/$action?/$id?" {
          controller = "admin${controllerClass.name[5..-1]}".toString()
        }
      }
    }
  }
}

I'd not actually done this before, your question prompted me to fix this is my app. It's been one of those things I've been trying to do for a while.


The grailsUrlMappingsHolder bean is available in services and controllers. Although it's concrete implementation of UrlMappingsHolder doesn't have an add method, its superclass does. Simple as this in grails 2.3.4

def grailsUrlMappingsHolder

def addMapping() {        
    grailsUrlMappingsHolder.addMappings({
        "/admin"(controller:"admin" action:"index")  
    });        
}


I wanted to achieve something similar for my application and found a nice way provided by grails. It goes like

name admin: "/admin/$cName/$action/$id?" {
    controller = {
        "admin" + params.cName.capitalize()
    }
}

Watch out, this does not work if you use $controller vs. $cName (or whatever you like to have there) and will throw a NullpointerException instead.

As a bonus you can then use the mapping name like

<g:createLink 
    controller="adminBackend" action="login" 
    mapping="admin" params="[cName:'backend']"
/>

to get a link to /admin/backend/login - Hope this helps!

Stay fresh!


You can embed the $controller variable, see the documentation.

static mappings = {
   "/helpdesk/$controller/$action?/$id?"()
}

BTW, the mappings to controllers and, optional, their views are enclosed by normal brackets (), not curly ones {}.

Such Groovy Scripts (as UrlMappings.groovy) are parsed by a ConfigSlurper instance, which finally converts them to a ConfigObject that implements Map. Admittedly, I'm either not sure how this parsing is accomplished in detail.

EDIT:

Here's an UrlMappings.groovy that comes somewhat near to what you want. (Search for "/$_ctrl/$_action/$id?".) The code, BTW, gets evaluated at runtime. Nevertheless, I haven't been able to put the grailsApplication to work.

Another idea was to add a javax.servlet.Filter to the web application, i.e., by subclassing Grails' UrlMappingsFilter.

0

精彩评论

暂无评论...
验证码 换一张
取 消