使用默认索引方法的Grails连字符URL转换器问题 [英] Grails hyphenated URL converter issue with default index method

查看:95
本文介绍了使用默认索引方法的Grails连字符URL转换器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Grails的文档,默认的URL映射(在控制器操作和URL)使用camel case。您可以轻松地将URL映射更改为使用带连字符的URL:

  grails.web.url.converter ='连字'

例如,HelloWorldController.showUsers会映射到:

  / hello-world / show-users 

在Grails ,你可以有一个按照惯例是索引的默认控制器动作。例如,如果你有一个名为index()的方法,那么下面的URL会碰到这个方法:

  / hello-world 

您可以创建链接到该URL的锚标记,如下所示:

 < g:link controller =HelloWorld> Go!< / g:link> 

我注意到一个奇怪的错误,如果我的控制器名称前缀只有一个单词,例如HelloController,然后:

 < g:link controller =Hello> Go!< / g:link> 

...将始终生成指向以下内容的网址:

  / hello / index 

...而不是:

  / hello 

我重构并将其重命名为其他单个字控制器,问题依然存在。我使用的是Grails 2.2.2,所以我升级到了Grails 2.3.4,并且很惊讶这个bug仍然存在。将控制器重命名为任何两个字词前缀,如HomePageController,HelloWorldController,OneTwoController等,现在是解决方法。



这是我第一次真正使用GSP。我做错了什么?

解决方案

URL是用户体验的一部分,应该是干净的,所以 index 问题真的很烦人。



这很不方便,但是您可以消除索引单个词控制器URL的一部分,通过映射每个页面并省略操作。
$ b grails-app / config / UrlMappings.groovy

  class UrlMappings {

static mappings = {
/ hello(controller: hello)
/ settings(controller:settings)
/ dashboard(controller:dashboard)
...


According to the Grails documentation, the default URL mapping (between controller actions and URLs) uses camel case. You can easily change the URL mapping to use hyphenated URLs:

grails.web.url.converter = 'hyphenated'

So for instance, HelloWorldController.showUsers would map to:

/hello-world/show-users

In Grails, you can have a default controller action which by convention is "index". So for instance, if you have a method named index(), the following URL will hit that method:

/hello-world

You can create an anchor tag which links to that URL like this:

<g:link controller="HelloWorld">Go!</g:link>

I noticed a strange bug where if my controller name prefix is only one "word" such as HelloController, then:

<g:link controller="Hello">Go!</g:link>

... will always generate URLs which point to:

/hello/index

... instead of:

/hello

I refactored and renamed it to other single word controllers and the problem persisted. I was using Grails 2.2.2 so I upgraded to Grails 2.3.4 and was surprised this bug still existed. Renaming the controller to any two word prefix, like HomePageController, HelloWorldController, OneTwoController, etc, is a workaround for now.

This is my first time really using GSP. Am I doing something wrong?

解决方案

URLs are part of the user experience and should be clean, so the index problem is really annoying.

It's inconvenient, but you can eliminate the index part of the URL for single word controllers by mapping each page and omitting the action.

grails-app/config/UrlMappings.groovy

class UrlMappings {

   static mappings = {
      "/hello"     (controller: "hello")
      "/settings"  (controller: "settings")
      "/dashboard" (controller: "dashboard")
      ...

这篇关于使用默认索引方法的Grails连字符URL转换器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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