【问题标题】:Grails - hide the /index Action using RoutesGrails - 使用路由隐藏 /index 操作
【发布时间】:2011-10-18 13:22:02
【问题描述】:

在 Grails 应用程序中使用路由隐藏 /index 操作的正确方法是什么?

我希望能够重定向到控制器:“配置文件”,操作:“索引”,但 url 看起来像
http://foobar.com/profile
不是
http://foobar.com/profile/index

【问题讨论】:

标签: grails routes url-routing


【解决方案1】:

UrlMappings.groovy

static mappings = {
      "/profile"(controller:"profile", action: "index")
}

或者您可以在控制器中设置默认操作

class BookController {
    static defaultAction = "index"
}

如果您想重定向到该 URL,那么从控制器中的操作中..

redirect uri: '/profile'  // This one for the UrlMappings solution

redirect controller: 'profile'  // This one for the defaultAction solution.

【讨论】:

  • 在 Grails 2.2.3 中,defaultAction 变通方法不幸地将不需要的 index 附加到 <g:link controller="profile"> 的 URL。 mappings 解决方法很好,可以缩短为:"/profile" (controller: "profile")
【解决方案2】:
"/foo/" (controller:"foo", action:"index")

这在 UrlMapping.groovy 中。 UrlMapping.groovy 双向工作,这意味着指向控制器 foo 和操作索引的 g:link 也将生成缩短的 url。

您需要为每个控制器索引执行此操作。

我试过设置:

"/$controller" {}

但没有雪茄,也许其他人知道这是怎么回事。

编辑: 哈,我在尼克之前开始写这个答案,然后花了 10 分钟测试了 $controller 东西的各种组合:-)

【讨论】:

    猜你喜欢
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多