我可以這樣來配置我的網址映射:Grails控制器應該實現所有的url映射嗎?
class UrlMappings {
static mappings = {
"/"(controller:"index")
"/$controller/$action?/$id?(.${format})?"{
constraints {
// apply constraints here
}
}
"500"(view:'/error')
////////////////////////
"/recetas"(resources:"receta",version:'1.0',namespace:'user'){
"/eventos"(resources:'evento',version:"1.0",namespace:"user")
}
}
}
此URL映射生成此URL映射報告。
|URL Mappings Configured for Application
|---------------------------------------
Dynamic Mappings
| * | /${controller}/${action}?/${id}?(.${format)? | Action: (default action) |
| * | ERROR: 500 | View: /error |
Controller: dbdoc
| * | /dbdoc/${section}?/${filename}?/${table}?/${column}? | Action: (default action) |
Controller: evento
| GET | /recetas/${recetaId}/eventos | Action: index |
| GET | /recetas/${recetaId}/eventos/create | Action: create |
| POST | /recetas/${recetaId}/eventos | Action: save |
| GET | /recetas/${recetaId}/eventos/${id} | Action: show |
| GET | /recetas/${recetaId}/eventos/${id}/edit | Action: edit |
| PUT | /recetas/${recetaId}/eventos/${id} | Action: update |
| DELETE | /recetas/${recetaId}/eventos/${id} | Action: delete |
Controller: index
| * |/ | Action: (default action) |
Controller: receta
| GET | /recetas | Action: index |
| GET | /recetas/create | Action: create |
| POST | /recetas | Action: save |
| GET | /recetas/${id} | Action: show |
| GET | /recetas/${id}/edit | Action: edit |
| PUT | /recetas/${id} | Action: update |
| DELETE | /recetas/${id} | Action: delete |
而且我EventoController
package com.ar.dasding
import grails.converters.JSON;
import grails.plugin.springsecurity.annotation.Secured;
import grails.transaction.Transactional;
@Transactional(readOnly = true)
@Secured(["ROLE_ADMIN"])
class EventoController {
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
def receta = Receta.get(params.recetaID)
log.debug("Eventos para receta " + params.recetaID + ": " + receta.eventos)
render receta.eventos as JSON
}
}
的recetaController工作正常,但提出這個要求來EventoController返回404
Request URL:http://localhost:8080/DasDingProject/recetas/1/eventos
Request Method:GET
Status Code:404 Not Found
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:es-ES,es;q=0.8,en;q=0.6
Connection:keep-alive
Cookie:JSESSIONID=30283EAC18BD88238A12449E825D8127; grails_remember_me=bWU6MTM5MTA4OTY5Njk3MTpmNmM3N2Q5MWU3ZTU3ZGE0OGRhZjAzNDViYzU5NDg3Yg
Host:localhost:8080
Referer:http://localhost:8080/DasDingProject/userHome
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36
X-Requested-With:XMLHttpRequest
Response Headersview parsed
HTTP/1.1 404 Not Found
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1015
Date: Thu, 16 Jan 2014 14:04:04 GMT
是否可能的問題是控制器不執行映射在url映射中的所有操作?
在此先感謝。
我知道出了什麼問題,命名空間的定義是使控制器的路由不工作。我無法使命名空間起作用,但將它們取出使得路由工作成爲可能。 – Sebete
urlmappings的順序也很重要 - 你的資源應該在catchall $ controller/$ action映射之上。 – tmarthal