多个grails url映射

时间:2012-08-31 10:00:42

标签: grails url-routing

在我的Grails应用程序中,我有以下控制器&动作

class FestivalController {

  def show() {
    [festival: Festival.get(params.id)]
  }
}

我希望以下所有网址都映射到此控制器

/festival/show/1
/festival/show/1/glastonbury
/1/music/glastonbury

其中glastonburymusic分别是节日的名称和类型。请注意,实际上只需要ID(1)来识别节日,因此URL中包含名称和类型,以及SEO(可读性)的原因。

我尝试使用以下网址映射支持这些不同的网址

// this supports the 3rd mapping above
name showFestival: "/$id/$type?/$name?" {
    controller = "festival"
    action = "show"
}

// this supports the 1st mapping above
"/$controller/$action?/$id?/$name?"{
    constraints {           
    }
}

这些支持第1和第3个URL映射,但如果我尝试第2次

/festival/show/1/glastonbury

它不起作用。理想情况下,我希望Grails始终生成以下形式的URL:

/1/music/glastonbury

当我使用g.createLinkg.link时,我还希望以下网址映射到此操作(由于历史原因):

/festival/show/1
/festival/show/1/glastonbury

1 个答案:

答案 0 :(得分:3)

在没有看到你的其他映射规则的情况下,很难知道什么是相关的,什么不是......你所看到的行为与通常的优先规则不一致,我extracted from the source a while back并且说当两个映射可以应用于相同的传入URI,获胜的是具有以下内容的URI:

  1. 较少的双通配符(**$var**),或者如果两者相等则
  2. 较少的单个通配符(*$var),或两者相等,则
  3. 更多非通配符路径段,或两者均等于
  4. 最左边的通配符(/foo/*/baz节拍/foo/bar/*),或者如果两者的最左边的通配符在同一个地方那么
  5. 更多constraints
  6. 的人

    根据这些规则,/festival/show/1/glastonbury仅匹配第二个映射,因此应该正常工作,但/festival/show/1匹配两者,因此应由/$id/$type/$name(较少的通配符)选取,从而导致[controller:'festival', action:'show', id:'festival', type:'show', name:'1']

    "/festival/$action?/$id?/$name?"(controller:'festival')添加显式规则应解决问题,因为/festival/show/1将匹配此映射和/id/type/name映射,但显式/festival/...映射具有更多非通配符路径段(1对无)。

相关问题