2013-07-22 86 views

回答

4

如果你註定的Grails的較舊版本(例如< 2.3),以及可用的插件不工作了,你可以使用named URL mappings產生有效寧靜的映射。

下面是我的一個項目的例子 - 我已經遺漏了一些細節,但希望這可以讓你開始如果你決定嘗試這種方法。

在你UrlMappings.groovy

/** 1. Mappings can handle multiple actions depending on HTTP 
    method like Rest. Names are a little clunky, like this would 
    be more appropriate as "resource" vs "showResource" but we didn't want 
    potential naming conflict in future release 

    2. TODO: DRY constraints - make constraints global 
    3. make sure controllers have proper actions defined 
*/ 

/** RESTFUL mapping for single resource */ 
name listResources: "/$controller" { 
    action = [GET: "list", POST: "save"] 
} 
name createResource: "/$controller/create" { 
    action = [GET: "create" ] 
} 
name deleteResource: "/$controller/$id?/delete" { 
    action = [POST: "delete", DELETE: "delete"] 
    constraints { id(matches: /[0-9]+/) } 
} 
name editResource: "/$controller/$id?/edit" { 
    action = [GET: "edit", PUT: "update", POST: "update"] 
    constraints { id(matches: /[0-9]+/) } 
} 
name showResource: "/$controller/$id?" { 
    action = [GET: "show", PUT: "update", POST: "update", DELETE: "delete"] 
    constraints { id(matches: /[0-9]+/) } 
} 

/** RESTFUL mapping for CHILD with PARENT */ 
name listChildResources: "/$parentResource/$pid/$controller" { 
    action = [GET: "list", POST: "save"] 
    constraints { pid(matches: /[0-9]+/) } 
} 
name createChildResource: "/$parentResource/$pid/$controller/create" { 
    action = [GET: "create" ] 
    constraints { pid(matches: /[0-9]+/) } 
} 
name showChildResource: "/$parentResource/$pid/$controller/$id?" { 
    action = [GET: "show", PUT: "update", POST: "update", DELETE: "delete"] 
    constraints { 
    id(matches: /[0-9]+/) 
    pid(matches: /[0-9]+/) 
    } 
} 
name editChildResource: "/$parentResource/$pid/$controller/$id?/edit" { 
    action = [GET: "edit"] 
    constraints { 
    id(matches: /[0-9]+/) 
    pid(matches: /[0-9]+/) 
    } 
} 

確保你控制器具有操作和支持的HTTP方法定義,如

static allowedMethods = [ 
    save: "POST", 
    update: ["POST", "PUT"], 
    delete: ["POST", "DELETE"] 
] 

然後使用映射像這樣(例如,假設我們已經花園和植物作爲資源)。

//show a garden 
<g:link mapping="showResource" controller="garden" 
    id="${gardenInstance.id}">${gardenInstance.name}</g:link> 

//create a plant for garden 
<g:link mapping="createChildResource" controller="plant" 
    params="[parentResource: 'garden', pid: gardenInstance.id]">Add Plant</g:link> 

//show list of plants within a garden 
<g:link mapping="listChildResources" controller="plant" 
    params="[parentResource: 'garden', pid: gardenInstance.id]">List plants for Garden</g:link> 

這裏顯示它是相當詳細的,但你可以把所有這一切都放到TagLib中,並有類似的東西。

<g:restShow resource="garden" 
    id="${gardenInstance.id}">${gardenInstance.name}</g:restShow> 

<g:restCreate" resource="plant" 
    parent="${gardenInstance}">Add Plant</g:restCreate> 
+0

這工作!非常感謝你。 – syllepsa

6

參見here。 Grails 2.3將支持嵌套的RESTful URL。

+0

感謝您的信息,但正如您所說,這將包括在即將發佈的版本中。有什麼插件可以在Grails 2.2中做同樣的事情嗎? – syllepsa

+0

我不知道。我對此表示懷疑。有關這方面的更多信息:http://stackoverflow.com/q/11657392 –