I organized my url mappings in grails with groups.
Example from UrlMappings:
class UrlMappings {
static mappings = {
group "/rest", {
"/" {
controller = "rest"
action = "index"
}
"/method1" {
controller = "rest"
action = "method1"
}
}
"/webservice/" {
controller = "webservice"
action = "index"
}
}
What I want: When the Url is called /rest/notexists, I want a 404 for this path. But only for /rest group, so the 404 should belong to the group rest. Other 404 are handled by my backbone router.
Any ideas?
I solved it with a catch all at the end of the rest group:
"/**" {
controller = "rest"
action ="notFound"
}
Related
I use the following url mapping in Grails:
"/$id"{
controller = "user"
action = "show"
}
to map urls like mydomain.com/someusername
How do I attach constrains to the url mapping to exclude keywords like "login", "logout",...
I.e.,
mydomain.com/someusername should route to mydomain.com/user/show/someusername,
mydomain.com/login should not route to mydomain.com/user/show/login.
You can use contrainsts for this mapping:
"/$id"{
controller = "user"
action = "show"
constraints {
//add a validator for $id from url mapping
id(validator: {
return !(it in ['login', 'logout'])
})
}
}
Use a filter, and redirect from it.
class UrlMappingFilters {
def filters = {
filterUrlKeywords(controller: '*', action: '*') {
def keywords = ['login', 'logout']
before = {
if (params.id in keywords) {
// redirect to start, or render error...
redirect(uri: '/')
return false
}
}
}
}
}
If you want to make it specific to a controller or action, use its name instead of the '*'.
Read more:
Official docs on Grails filters
Reference
I have done this type of thing for REST based endpoints numerous times, and Grails is smart enough to figure out what you want. Note that ordering in the UrlMappings file may be important.
For example, you can define this:
class UrlMappings {
static mappings = {
"/login" { controller: 'auth', action: 'login' }
"/logout" { controller: 'auth', action: 'logout' }
"/$id" { controller: 'user', action: 'view' }
}
}
Then when you hit "/login" you will go in the auth:login method, but when you hit "/userid" you will be sent to user:view:userid as expected.
I have the following:
"/api/users"(controller: "user") {
action = [GET:"list"]
}
Doing a call to http://localhost:8080/platform/users I get a list of users back. Then I added this:
"/api/users"(controller: "user") {
action = [POST:"save"]
}
And now I get a 404 and it is not hitting either method in UserController. I'd like to be able to use the same URL with the verb controlling which action. Am I doing this wrong or does Grails not support this?
From the Grails docs: URL Mappings
static mappings = {
"/product/$id"(controller:"product") {
action = [GET:"show", PUT:"update", DELETE:"delete", POST:"save"]
}
}
For your case:
"/api/users"(controller: "user") {
action = [GET:"list",POST:"save"]
}
Check your userController to see if there is allowedMethods defined accordingly like this:
class UserController {
static allowedMethods = [save: "POST", list: "GET"]
def list() {
.....
}
def save() {
.....
}
}
I'm using Grails 1.3.6. I have this file ...
grails-app/views/home/design/index.gsp
Here is what is defined in my HomeController. Sadly, whenever I visit, "http://localhost:port/context-path/design/", I get a 404 error. The server starts normally and there are no errors in the logs. What can I do to get my page instead of the 404?
def index = {
def folder = params.folder;
def page = params.page;
if (page) {
try {
def contents = IOService.getFileContents(folder, page)
response.setContentType("application/json")
response << contents
} catch (FileNotFoundException e) {
response.status = 404;
} // try
} else {
render(view: "/home/${folder}/index")
} // if
}
My URLMappings file consists of ...
static mappings = {
"/$folder?/$page"{
controller = "home"
action = "index"
}
"/"(view:"/index")
"500"(view:'/error')
}
Thanks, - Dave
If you want to be able to access
/context-path/home/design
Your action needs to be named design, i.e.
class HomeController {
def design = {
}
}
The Grails convention is always /context-path/controllerName/actionName (unless you have it mapped differently in grails-app/conf/URLMappings.groovy).
Your example's a bit unclear which path you're trying to access. To address both:
If you want /context-path/design, you need a DesignController with an index action (because if no action is supplied in the URL, Grails looks for the index action).
If you want /context-path/home/design, you need a HomeController with a design action.
Edit:
In the comments, you express the want to be able to have /context-path/design map to the HomeController index action. You can do this with grails-app/conf/URLMappings.groovy:
"/design"(controller: 'home', action: 'index')
Since it seems you have two distinct actions, I would set things up a bit differently:
def indexWithPage = {
def folder = params.folder;
def page = params.page;
try {
def contents = IOService.getFileContents(folder, page)
response.setContentType("application/json")
response << contents
} catch (FileNotFoundException e) {
e.printStackTrace();
response.status = 404;
} // try
}
def index
def folder = params.folder;
render(view: "/home/${folder}/index")
}
with a URLMaping of:
static mappings = {
"/$folder/$page"{
controller = "home"
action = "indexWithPage"
}
"/$folder"{
controller = "home"
action = "index"
}
"/"(view:"/index")
"500"(view:'/error')
}
I also threw a e.printStackTrace(); in there to help us determine whether you're getting YOUR 404 or the action is truely not being called.
I'm running Grails 1.3.6. I have this in my URLMappings.groovy file ...
static mappings = {
"/$folder?/$page?"{
controller = "Home"
action = "index"
}
"/"(view:"/index")
"500"(view:'/error')
}
and this is my HomeController ...
class HomeController {
def IOService
def index = {
def folder = params.folder;
def page = params.page;
def contents = IOService.getFileContents(folder, page)
response.setContentType("application/json")
response.text = contents
}
}
however, when I visit my URL "/context-path/folder1/page1", I'm getting an Apache Tomcat 404 error (complaining about "/context-path/folder1"). I'm new to Grails but can't figure this out. How can I adjust my mappings to make this work?
Thanks, - Dave
I'm not sure if controller/action definitions are case-insensitive. I've always used lowercase names. Try changing
controller = "Home"
to
controller = 'home'
How do I create a catch-all URL-mapping in Grails?
The following Grails UrlMapping ..
class UrlMappings {
static mappings = {
"/$something"{
controller = "something"
action = "something"
}
}
}
.. appears to match ^/[^/]* but how do I create an UrlMapping matching all URLs (^/.*)?
You're looking for the ** "double wildcard". Example:
class UrlMappings {
static mappings = {
"/**"(controller: "something", action: "something")
}
}