多个Grails网址映射 [英] multiple grails url mappings

查看:89
本文介绍了多个Grails网址映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Grails app, I have the following controller & action

class FestivalController {

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

我想全部映射到此控制器的以下网址

I would like all of the following URLs to map to this controller

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

其中 glastonbury 音乐分别是节日的名称和类型。请注意,只有ID(1)实际上需要用来标识节日,所以名称和类型包含在URL中,原因在于SEO和URL的可读性。

where glastonbury and music are the name and type of the festival respectively. Notice that only the ID (1) is actually needed to identify the festival, so the name and type are included in the URL for reasons of SEO and readability (of the URLs).

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

I attempted to support these different URLs with the following URL mappings

// 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 {           
    }
}

这些支持第一个和第三个URL映射,但是如果我尝试第二个

These support the 1st and 3rd URL mappings, but if I try the 2nd

/festival/show/1/glastonbury

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

it doesn't work. Ideally, I would like Grails to always generate a URL of the form:

/1/music/glastonbury

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

when I use g.createLink or g.link, but I would also like the following URLs to map to this action (for historical reasons):

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


推荐答案

没有看到你的其他映射规则,很难知道什么是相关的,什么是不相关的......你看到的行为与通常的优先规则不一致,我的从源代码中提取一段时间回来,其中说,当两个映射可以应用于相同的传入URI,胜出的是具有以下内容的URI:

Without seeing your other mapping rules it's hard to know what's relevant and what isn't... The behaviour you're seeing is inconsistent with the usual precedence rules, which I extracted from the source a while back and which say that when two mappings could apply to the same incoming URI, the one that wins is the one that has:


  1. 更少的双通配符( ** $ var ** ),或者如果两个ar e等于那么
  2. 更少的单个通配符( * $ var ),或者如果两者相等,那么

  3. 更多非通配符路径段,或者两者相等,则

  4. 最远的通配符( / foo / * / baz 敲击 / foo / bar / * ),或者如果两者的最左侧通配符位于相同位置, / li>
  5. 更多约束

  1. Fewer double wildcards (** or $var**), or if both are equal then
  2. Fewer single wildcards (* or $var), or if both are equal then
  3. More non-wildcard path segments, or if both are equal then
  4. The furthest left wildcard (/foo/*/baz beats /foo/bar/*), or if both have their leftmost wildcard in the same place then
  5. The one with more constraints

通过这些规则, / festival / show / 1 / glastonbury 仅匹配第二个映射,所以应该正常工作,但 / festival / show / 1 匹配两者,所以应该由 / $ id / $ type / $ name (更少的通配符)提取,结果是 [controller:'festival',action:'show',id:'festival',type:'show',name:'1']

By these rules, /festival/show/1/glastonbury matches only the second mapping, so should work correctly, but /festival/show/1 matches both, so should be picked up by /$id/$type/$name (fewer wildcards), resulting in [controller:'festival', action:'show', id:'festival', type:'show', name:'1'].

/ festival / $ action?/ $ id?/ $ name?(控制器:'festival')添加显式规则应该将事情解决为 / festival / show / 1 将匹配这个映射a nd / id / type / name 一个,但显式的 / festival /...通配符路径段(1 vs none)。

Adding an explicit rule for "/festival/$action?/$id?/$name?"(controller:'festival') should fix things as /festival/show/1 will match both this mapping and the /id/type/name one, but the explicit /festival/... mapping has more non-wildcard path segments (1 vs none).

这篇关于多个Grails网址映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆