我正在尝试基于grails项目的域类构建一些动态URL映射。目标是使用单个通用 Controller ,但URL决定使用哪个域进行处理。现在的问题是,当使用<g:link />
标记时,我没有得到想要的URL。
我尝试了以下变体来创建URL映射:
static mappings = {
Holders.grailsApplication.domainClasses.each {
"/admin/${it.propertyName}/$action?/$id?"(controller: "admin")
}
}
static mappings = {
"/admin/$domainClass/$action?/$id?"(controller: "admin")
}
这两种变体均可用于实际的URL匹配。但是我个人不喜欢grails进行反向URL映射的行为。对于变体1,反向映射始终解析为AdminController最后添加的URL映射。对于情况2,我有一个问题,即使理论上不必要,我也必须将domainClass-Parameter传递给每个链接创建调用,因为当前请求中已经存在该信息。
我知道有可能使用命名URL映射,然后再使用
<g:link mapping="mapping_name" />
之类的东西。问题是我正在使用一些通用的应用程序范围的局部 View ,在该 View 中,我尝试仅提供创建链接所需的信息,例如<g:link action="something" />
。这引出了我两个问题:
最佳答案
您可以定义命名映射,例如
Holders.grailsApplication.domainClasses.each { dc ->
name((dc.propertyName):"/admin/${dc.propertyName}/$action?/$id?" {
controller = "admin"
domainClass = dc.propertyName
})
}
将映射名称保存在参数中后,您现在可以执行
<g:link mapping="${params.domainClass}">link text</g:link>
关于grails - Grails反向网址映射:有没有一种方法可以根据当前匹配的路线建立链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25354697/