代码之家  ›  专栏  ›  技术社区  ›  DavidC

动态Grails URL映射配置

  •  11
  • DavidC  · 技术社区  · 14 年前

    如何动态构建映射列表-而不是:

    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")
     }
    }
    

    类似这样的伪代码:

    class UrlMappings {
    static mappings = {
       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}")
       }
     }
    }
    

    (我不明白静态映射是什么——散列映射?自由变量?)

    我试图实现的是基于控制器类型的映射-例如,帮助台、管理员或用户控制器。设置映射后,我希望根据URL添加安全性,但不希望单独映射每个控制器:

    grails.plugins.springsecurity.interceptUrlMap = [
       '/helpdesk/**':   ['ROLE_HELPDESK','ROLE_ADMIN'],
    ]
    
    4 回复  |  直到 11 年前
        1
  •  11
  •   Gareth Davis    14 年前

    我刚刚在我的应用程序中完成了以下操作:

    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()
            }
          }
        }
      }
    }
    

    我以前没有做过,你的问题让我修正了这是我的应用程序。这是我一直想做的事情之一。

        2
  •  5
  •   sparkyspider    11 年前

    grailsurlmappingsholder bean在服务和控制器中可用。虽然UrlMappingSholder的具体实现没有add方法,但它的超类有。在Grails2.3.4中就这么简单

    def grailsUrlMappingsHolder
    
    def addMapping() {        
        grailsUrlMappingsHolder.addMappings({
            "/admin"(controller:"admin" action:"index")  
        });        
    }
    
        3
  •  2
  •   fluxon    12 年前

    我想为我的应用程序实现类似的功能,并找到了Grails提供的一种好方法。就像是

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

    当心,如果你使用 $controller VS $cName (或者任何你想拥有的东西)并将抛出一个空指针异常。

    作为奖励,您可以使用如下映射名称

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

    要获得/admin/backend/login的链接-希望这有帮助!

    保持新鲜!

        4
  •  0
  •   robbbert    14 年前

    你可以嵌入 $controller 变量,请参见 documentation .

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

    顺便说一句,映射到 controller s和,可选,它们的 view S用普通括号括起来 () 不是卷发的 {} .

    这样的groovy脚本(如 urlmappings.groovy公司 )由分析 ConfigSlurper 实例,它最终将它们转换为 ConfigObject 那个 implements Map . 不可否认,我也不确定这个解析是如何详细完成的。

    编辑 :

    Here 是一个 urlmappings.groovy公司 这有点接近你想要的。(搜索“/$\u ctrl/$\u action/$id?”.)代码btw在运行时得到评估。不过,我还没能把 grailsApplication 去工作。

    另一个想法是增加 javax.servlet.Filter 到Web应用程序,即通过对Grails的子类化 UrlMappingsFilter .