我與Grails 2.1.1
工作,想添加自定義的URL映射到控制器動作屈指可數。Grails的urlMapping中重定向保持乾燥
我可以做到這一點,但原始的映射仍然有效。
例如,我在創建UrlMappings
映射add-property-to-directory
如下:
class UrlMappings {
static mappings = {
"/add-property-to-directory"(controller: "property", action: "create")
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
現在,我可以打/mysite/add-property-to-directory
,它會執行PropertyController.create
,正如我所期望的。
但是,我仍然可以按/mysite/property/create
,它會執行相同的PropertyController.create
方法。
本着DRY的精神,我想做一個301重定向從/mysite/property/create
到/mysite/add-property-to-directory
。
我找不到在UrlMappings.groovy
中這樣做的方法。有沒有人知道我可以在Grails中實現這一點的方法?
非常感謝!
UPDATE
這裏是我實現的解決方案的基礎上,湯姆的回答是:
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)
}
}
目前尚不可能。有一項功能請求可以指定URL映射中的重定向 - 請參閱http://jira.grails.org/browse/GRAILS-5994 –
@sudhir謝謝,這回答了我的問題。您能否將您的評論複製到答案中,以便我可以接受它? –
@sudhir,謝謝你的評論和有用的鏈接......湯姆編輯了他的答案,它引導我走在正確的軌道上去做所尋找的事情,所以我接受了他的答案。 –