2011-05-25 126 views
2

我已經在Grails的配置一些RESTful資源這樣Gerneric URL映射Grails的RESTful資源

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

但我想這更通用像

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

不工作。 ..(越來越404)

我如何配置一個通用的網址映射爲rest資源在grails中?

回答

2

這似乎映射的「資源」部分在啓動時進行評估,而不是實際的請求的執行(從這個玩弄一個猜想)。因此,我認爲您需要基於應用程序啓動時提供的Domain類來動態「預加載」您所需的一組UrlMappings。像這樣的東西可能做的伎倆:

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

你的猜測可能是對的,你的解決方案有效,thx。但是,如果grails評估每個請求的資源映射,就像控制器/操作映射一樣,那將會非常簡潔 – aveltens 2011-05-27 07:20:34

0

您需求的正確映射完全取決於您試圖達到的目標(無法從問題中確定)。

考慮一個通用的映射:

"/$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. 
+0

謝謝,但我不希望映射到行動,但以資源的問題,13.1「休息」的Grails用戶[指南]解釋(HTTP:/ /grails.org/doc/latest/guide/13.%20Web%20Services.html#13.1%20REST)。但是我不想明確地列出所有的資源(書,作者等),我想以一種通用的方式來完成它,就像你爲動作所做的解釋一樣(因爲你沒有明確指出動作) – aveltens 2011-05-25 18:45:33