Grails app index page URL - grails

I am trying to shorten the URL to get to my grails app. Currently the shortest I can get it to is
http://myserver:8080/helloWorld/helloWorld/
HelloWorld is the controller name and the app name. Can I make it shorter somehow so its only
http://myserver:8080/helloWorld/
I have URL mappings set to
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}

You can make HelloWorldController the default URL by changing
"/"(view:"/index")
to
"/"(controller: 'helloWorld')
This uses the default action in that controller (likely index()); if you want a different action, do this:
"/"(controller: 'helloWorld', action: 'theOtherName')

If you only have one controller, there's no need to have it in the URL. You could use the following mapping:
static mappings = {
"/$action?/$id?"(controller:'helloWorld')
"500"(view:'/error')
}
In this case, http://myserver:8080/helloWorld/ will go to HelloWorldController.index() instead of serving up the index.gsp view.
The leading helloWorld is also optional. Add these lines to your Config.groovy to use the root context:
grails.app.context = "/"
grails.serverURL = "http://myserver:8080"
Combining both of these will allow you app be accessed through http://myserver:8080/.

Related

Parameter 'id' to be query string in Grails redirection

From Grails controller, I want to redirect to this (using query string):
/mycontroller/myaction?id=3
I wrote this code:
def a = 3;
redirect (controller:"mycontroller",action:"myaction",params:[id:a])
This code will produces:
/mycontroller/myaction/3
I know that 'id' is special parameter. It will be url not query string. I try another parameter
def name = "John";
redirect (controller:"mycontroller",action:"myaction",params:[name:name])
will produces:
/mycontroller/myaction?name=John
Your described behavior is a result of your UrlMappings configuration.
If you use the default mapping, the id parameter will be put in the at the described position $id?:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
In general this is no problem. You could also use the id parameter as if it was set as query string:
def myaction() {
def idFromParams = params.id
}
Or you simply rewrite your UrlMappings.

Grails UrlMapping route to fixed controller and action

I made the following rule in my UrlMapping file and now all my controllers are matching to the ("/$username") mapping, not the first one ("/$controller/$action?/$id?").
The idea here was to list all public items from an user using a short url. It works but it breaks all others controllers.
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/$username" {
controller = 'user'
action = 'publicItens'
}
"/"(controller:'usuario', action: 'index' )
"500"(view:'/error')
}
How can I map it correctly?
solved!
I just wrote some code in the UrlMappings to create rules automatically for each controller in the application. Using this approach when the user types /appname/controllerName then the rule created automatically is considered in place of the "$/username" rule.
The critical point is that the use of ApplicationHolder which is deprecated. That can fixed writing your own ApplicationHolder.
static mappings = {
//creates one mapping rule for each controller in the application
ApplicationHolder.application.controllerClasses*.logicalPropertyName.each { cName ->
"/$cName" {
controller = cName
}
}
"/$controller/$action?/$id?"{
}
"/$username" {
controller = 'usuario'
action = 'itensPublicos'
}
"/"(controller:'usuario', action: 'index' )
"500"(view:'/error')
}
Just add /users/$username to the URL mapping. This is the simplest way how to achieve your goals.
"/users/$username" {
controller = 'user'
action = 'publicItens'
}
You could probably also exclude the user and usario controllers in the first url mapping constraints(notEqual)
http://www.grails.org/doc/latest/ref/Constraints/notEqual.html

Setting up the filter without controllers

I have a running tomcat at localost.
I wish to write a grails filter such that when ever user goes to localhost/filter/ intercept the call and do some processing. I created a filter which is inside conf folder
class TestFilters {
def filters = {
filter(uri:'/filter') {
before = {
}
after = {
}
afterView = {
}
}
}
}
after setting this up when I go to localhost/filter/ I get only 404 error.
Where I'm making the mistake?
Thanks in advance
If you have no FilterController the url localhost/filter has no ressource to show - so you get a 404 error. You have to adapt your UrlMappings so that localhost/filter is a valid url of application.
Add the following to UrlMappings.groovy:
"/filter" (controller: "yourController", action:"yourAction")
Now - the url localhost/filter points to yourAction in yourController and the filter should be applied.

Grails 1.3.7 picks wrong view for controller

I have controller named LoginController with action auth:
class LoginController {
def auth = {
render view: 'auth'
}
}
So I go to the URL http://localhost:8080/myapp/login/auth, and want to see my view '/login/auth.gsp'. But I get the following error:
type Status report
message /myapp/WEB-INF/grails-app/views/jobSearch/auth.jsp
description The requested resource (/myapp/WEB-INF/grails-app/views/jobSearch/auth.jsp) is not available.
Seems that grails resolves wrong view name for some reason. Do you know, what's going on?
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints { // apply constraints here }
}
"/"(view:"/index") "500"(view:'/error')
}
}
Agree with #omarello, something looks like its mapping the login controller URL to 'jobsearch'. The error message relating to JSP is what happens when it can't find a matching gsp page and tries to fallback to JSP.

grails url mapping

i am new to grails & want to know:-
how to get the default grails url mapping in grails? Where are they stored? how can the default url mappings replaced by our custom url mappings from the grails controller?
URLMapping is done in the urlmappings.groovy file. Read more on how to map on http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.4%20URL%20Mappings
Here is an example of UrlMappings
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
See the latest documentation:
https://grails.github.io/grails-doc/latest/ref/Plug-ins/URL%20mappings.html

Resources