动态 Grails Url 映射配置 [英] Dynamic Grails Url Mapping config

查看:18
本文介绍了动态 Grails Url 映射配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

类似这样的伪代码:

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

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

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

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

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.

这篇关于动态 Grails Url 映射配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆