Grails脚手架链接参考问题

发布于 2024-08-22 13:29:36 字数 473 浏览 5 评论 0原文

我正在为两个域类的几个控制器使用脚手架:1 个扇区到 N 个项目:

class Item {

String name

static belongsTo = [sector:Sector]

....

}

class Sector {

String name

static hasMany = [items:Item]

....

}

当我生成相应的脚手架控制器时,我使用了模式(类)mgr:Sectormgr.groovy 和 Itemmgr.groovy。

问题是某些链接在某些生成的视图中无效,因为它假设 I 遵循控制器的默认名称。例如:

  • 如果我转到 /sectormgr/show/20,与其关联的项目列表具有链接 /item/show/22,而不是 /itemmgr/show/22

有一个简单的解决方案吗?创建控制器时我是否遗漏了一些东西?

提前致谢

I'm using scaffolding for a couple of Controllers for two Domain Classes: 1 Sector to N Items:

class Item {

String name

static belongsTo = [sector:Sector]

....

}

class Sector {

String name

static hasMany = [items:Item]

....

}

When I generated the corresponding scaffolding controllers I used the pattern (class)mgr: Sectormgr.groovy and Itemmgr.groovy.

The problem is that some links are invalid in some of the generated views, for it is assuming the I followed the default names for the controllers. For instance:

  • if I go to /sectormgr/show/20, the list of items associated with it have the link /item/show/22, instead of /itemmgr/show/22

Is there an easy fix for this? Am I missing something when I create the controllers ?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不疑不惑不回忆 2024-08-29 13:29:36

我相信有几种方法可以解决这个问题。最简单的方法是坚持 Grails 命名控制器的约定 SectorController.groovy、ItemController.groovy 等。

认为可以解决此问题的另一种方法是更新 grails-app/conf /UrlMappings.groovy。这是默认的脚手架:

class UrlMappings {
    static mappings = {
        "/$controller/$action?/$id?"{
            constraints {
                //apply constraints here
            }
        }
        "/"(view:"/index")
        "500"(view:'/error')
    }
}

您想要类似的东西:

class UrlMappings {
    static mappings = {
        "/${controller}mgr/$action?/$id?"{  //Add mgr after controller
            constraints {
                //apply constraints here
            }
        }
        "/"(view:"/index")
        "500"(view:'/error')
    }
}

There's a couple ways to address this I believe. The simplest is to stick to Grails' convention of naming your controllers SectorController.groovy, ItemController.groovy, etc.

One other way to handle this that I think will work is to update your grails-app/conf/UrlMappings.groovy. Here is the default scaffolding:

class UrlMappings {
    static mappings = {
        "/$controller/$action?/$id?"{
            constraints {
                //apply constraints here
            }
        }
        "/"(view:"/index")
        "500"(view:'/error')
    }
}

You want something like:

class UrlMappings {
    static mappings = {
        "/${controller}mgr/$action?/$id?"{  //Add mgr after controller
            constraints {
                //apply constraints here
            }
        }
        "/"(view:"/index")
        "500"(view:'/error')
    }
}
断肠人 2024-08-29 13:29:36

更改 URLMappings 似乎对我不起作用,而且这是一个更全局的更改。我采取的方法是运行 intall-templates 并更改 src/templates/scaffolding/show.gsp 中控制器的链接。

更改模板后,您需要重新启动服务器。

Changing the URLMappings didn't seem to work for me, and it's a much more global change. Running intall-templates and changing the links to controllers in src/templates/scaffolding/show.gsp was the approach I took.

You'll need to restart your server after changing the template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文