Dynamic Grails Url Mapping config

后端 未结 4 1669
醉酒成梦
醉酒成梦 2020-12-15 10:13

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

class UrlMappings {
static mappings = {
   \"/helpdesk/user/$action?/$id?\" (controller=\"helpde         


        
相关标签:
4条回答
  • 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!

    0 讨论(0)
  • 2020-12-15 11:00

    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.

    0 讨论(0)
  • 2020-12-15 11:15

    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")  
        });        
    }
    
    0 讨论(0)
  • 2020-12-15 11:15

    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 讨论(0)
提交回复
热议问题