问题描述
我如何动态生成映射列表 - 而不是:
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)
}
}
代码:
类UrlMappings {
的静态映射= {
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 = [
'/服务台/ **':[ 'ROLE_HELPDESK', 'ROLE_ADMIN'],
]
解决方案我在我的应用程序中完成了以下操作:
进口org.codehaus.groovy.grails.commons.ApplicationHolder
类UrlMappings {
的静态映射= {
为(controllerClass在ApplicationHolder.application.controllerClasses){
//管理员控制器首先
if(controllerClass.name.startsWith(Admin)){
// note ...修复案例,使AdminUserController映射到/ admin / user
/ admin / $ {controllerClass.name [5] .toLowerCase()+ controllerClass.name [6 ..- 1]} / $ action?/ $ id? {
controller =admin $ {controllerClass.name [5 ..- 1]}。toString()
}
}
}
}
}
我之前没有这样做过,您的问题促使我修复这是我的应用程序。这是我一直试图做的一件事。
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 = { 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.
这篇关于动态Grails Url映射配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!