grails 中静态资源的通用 url 映射

发布于 2024-11-09 20:56:59 字数 278 浏览 3 评论 0原文

我已经在 grails 中配置了一些静态资源,如下所示

"/book/$id?"(resource:"book")
"/author/$id?"(resource:"author")

,但我想要这个更通用的资源,但

"/$controller/$id?"(resource: controller)

不起作用...(获取 404)

如何为 grails 中的静态资源配置通用 url 映射?

I have configured some restful resources in grails like this

"/book/$id?"(resource:"book")
"/author/$id?"(resource:"author")

But I want to to this more generic like

"/$controller/$id?"(resource: controller)

which doesn't work... (getting 404)

How can i configure a generic url mapping for restful resources in grails?

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

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

发布评论

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

评论(2

穿透光 2024-11-16 20:56:59

看起来映射的“资源”部分是在启动时评估的,而不是在执行实际请求时评估的(通过对此进行猜测)。因此,我认为您需要根据应用程序启动时可用的域类动态“预加载”您想要的 UrlMappings 集。像这样的事情可能会起作用:

class UrlMappings {
static mappings = {
        ApplicationHolder.application.getArtefacts('Domain').each { GrailsClass cl ->
           "/${cl.propertyName}/$id?"(resource: cl.propertyName )            
        } ...

It would seem that the "resource" portion of the mapping is evaluated on startup, not on execution of the actual request (a guess from playing around with this). Therefore, I think you need to "preload" the set of UrlMappings you want dynamically based on the Domain classes available on application startup. Something like this might do the trick:

class UrlMappings {
static mappings = {
        ApplicationHolder.application.getArtefacts('Domain').each { GrailsClass cl ->
           "/${cl.propertyName}/$id?"(resource: cl.propertyName )            
        } ...
浅笑依然 2024-11-16 20:56:59

满足您需求的正确映射完全取决于您想要实现的目标(无法从问题中确定)。

考虑一个通用映射:

"/$controller/$action?/$id?"{}

这会将 URL 的第一部分映射到同名的控制器,第二部分映射到该控制器的操作,第三部分映射到域类实例的唯一标识符。

使用此映射的示例:

/book/show/2

Controller: bookController
Action: show
id: 2

This will call the show action of the book controller.
params.id will be 2.

The intention implied in the URL is to show relevant details for a book object 
with an id of 2.

/question/update/6

Controller: questionController
Action: update
id: 6

This will call the update action of the question controller.
params.id will be 6.

The intention implied in the URL is to update the details for a question
object with an id of 6.

The correct mapping for your needs depends entirely on what you are trying to achieve (which can't be determined from the question).

Consider a generic mapping:

"/$controller/$action?/$id?"{}

This will map the first part of the URL to a controller of the same name, the second part to an action of that controller and the third part to what should be a unique identifier for a domain class instance.

Examples that work with this mapping:

/book/show/2

Controller: bookController
Action: show
id: 2

This will call the show action of the book controller.
params.id will be 2.

The intention implied in the URL is to show relevant details for a book object 
with an id of 2.

/question/update/6

Controller: questionController
Action: update
id: 6

This will call the update action of the question controller.
params.id will be 6.

The intention implied in the URL is to update the details for a question
object with an id of 6.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文