Grails UrlMapping重定向以保持DRY [英] Grails UrlMapping Redirect to keep DRY
问题描述
我正在使用 Grails 2.1.1
,并希望添加一些映射到控制器操作的自定义URL。
我可以这样做,但原始映射仍然有效。
例如,我在中创建了一个映射
如下: add-property-to-directory
UrlMappings
class UrlMappings {
static mappings = {
/ add-property-to-directory(controller:property,action:create)
/ $ controller / $ action?/ $ id?{
constraints {
//在这里应用约束
}
}
/(view:/ index)
500(view:' / error')
}
}
现在,我可以点击 / mysite / add-property-to-directory
,它会执行 PropertyController.create
,如我所料。然而,我仍然可以打到 / mysite / property / create
,它会执行相同的 PropertyController.create
方法。
本着DRY的精神,我想做一个301重定向从 / mysite / property / create
到 / mysite的/附加属性到目录
。
我找不到在 UrlMappings.groovy
中执行此操作的方法。有没有人知道我可以在Grails中实现这一点的方法?
非常感谢!
更新
以下是我根据Tom的回答实施的解决方案:
UrlMappings.groovy
class UrlMappings {
static mappings = {
/ add-property-to目录(控制器:属性,操作:创建)
/ property / create{
controller =redirect
destination =/ add-property-to-目录
}
/ $ controller / $ action?/ $ id?{
constraints {
// apply constraints here
$ b/(view:/ index)
500(view:'/ error')
}
}
RedirectController.groovy
class RedirectController {
def index(){
redirect(url:params.de stination,permanent:true)
}
}
/ $ controller / $ action?/ $ id? (
controller:'myRedirectControlller',action:'myRedirectAction',params:[controller:$ controller,action:$ action,id:$ id]
)
/ user / list(controller:'user',action:'list')
你可以在params中得到normallny的值:
log.trace'myRedirectController.myRedirectAction:'+ params.controller +','+ params.action +','+ params.id
I am working with Grails 2.1.1
and would like to add a handful of customized URLs that map to Controller Actions.
I can do that, but the original mapping still works.
For example, I created a mapping add-property-to-directory
in my UrlMappings
as follows:
class UrlMappings {
static mappings = {
"/add-property-to-directory"(controller: "property", action: "create")
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
Now, I can hit /mysite/add-property-to-directory
and it will execute PropertyController.create
, as I would expect.
However, I can still hit /mysite/property/create
, and it will execute the same PropertyController.create
method.
In the spirit of DRY, I would like to do a 301 Redirect from /mysite/property/create
to /mysite/add-property-to-directory
.
I could not find a way to do this in UrlMappings.groovy
. Does anyone know of a way I can accomplish this in Grails?
Thank you very much!
UPDATE
Here is the solution that I implemented, based on Tom's answer:
UrlMappings.groovy
class UrlMappings {
static mappings = {
"/add-property-to-directory"(controller: "property", action: "create")
"/property/create" {
controller = "redirect"
destination = "/add-property-to-directory"
}
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
RedirectController.groovy
class RedirectController {
def index() {
redirect(url: params.destination, permanent: true)
}
}
It is possible to achieve this:
"/$controller/$action?/$id?" (
controller: 'myRedirectControlller', action: 'myRedirectAction', params:[ controller: $controller, action: $action, id: $id ]
)
"/user/list" ( controller:'user', action:'list' )
and in the action you get the values normallny in params:
log.trace 'myRedirectController.myRedirectAction: ' + params.controller + ', ' + params.action + ', ' + params.id
这篇关于Grails UrlMapping重定向以保持DRY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!