当涉及到 Grails 2.3时,这可以很容易地映射。
//Grails 2.3
"/color"(resources:'color') {
"/shades"(resources:"shade")
}
然后你可以访问/color/${id}/shades
当Grails 2.2.4 或更低版本时,我认为你UrlMapping
可以像下面这样优化:
class UrlMappings {
static mappings = {
"/colors/$id?/$shades?" (resource: "color"){
constraints {
shades validator: {
it in ['shades']
}
id validator: {it.isNumber()}
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
//controller action corresponding
//GET
def show(){
if(params.id){
if(params.shades){
//If both id and shades present in URL
//then getShades
//maps to "/colors/5/shades"
getShades()
} else {
//If only id provided then GET color
//maps to "/colors/5"
getColor()
}
} else {
//If id not provided the list all colors
//maps to "/colors"
listColors()
}
}
private def getShades(){...}
private def getColor(){...}
private def listColors(){...}
注意
注意删除 grails 提供的默认映射
//remove
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
删除默认条目的基本原理:-如果您删除此条目,则不需要(和)
的其他验证器,假设您没有使用 REST 服务的默认条目。create
detail