Grails UrlMapping route to fixed controller and action - grails

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

Related

Grails case insensitive URL Mapping of predefined URL?

I have the following url mapping:
"/Some-Name".toLowerCase() {
controller ="user"
action = "show"
id = "f274b72e1309467e70"
}
Note this is not a duplicate of this: How to make my URL mapping case insensitive? It is a different case.
How can I make this URL mapping case insensitive?
Maybe you can try to use custom validator in constraint section. Please note that code below is not tested.
"/$path" {
controller ="user"
action = "show"
id = "f274b72e1309467e70"
constraints {
path((validator: { return it.equalsIgnoreCase("Some-Name") })
}
}

Does Grails Filter `actionExclude` work with method-based routing?

I have a method-based route like:
name base: "/" {
controller="api"
action=[GET: "welcome", POST: "post"]
}
And I'd like to apply a filter to handle authorization, e.g.
class RequestFilters {
def filters = {
authorizeRequest(controller: 'api', actionExclude: 'welcome') {
before = {
log.debug("Applying authorization filter.")
}
}
}
}
But when I apply this in practice, the filter runs on all requests (even GET requests, which should use the welcome method and thus should not trigger this filter.)
When I inspect the code running the in the filter, I see that params.action is set to the Map from my routing file, rather than to "welcome". Not sure if this is related to the issue.
My current workaround (which feels very wrong) is to add the following to my filter's body:
if(params.action[request.method] == 'welcome'){
return true
}
The short question is: does Grails support this combination of method-based routing + action-name-based filtering? If so, how? If not, what are some reasonable alternatives for restructuring this logic?
Thanks!
You need to use the filter as below:
class RequestFilters {
def filters = {
authorizeRequest(controller:'api', action:'*', actionExclude:'welcome'){
before = {
log.debug("Applying authorization filter.")
return true
}
}
}
}
Apply the filter to all actions of the controller but "welcome". :)
If there is no other welcome action in the other controllers then, you would not need the controller specified in the filter as well.
authorizeRequest(action:'*', actionExclude:'welcome'){...}

Grails app index page URL

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/.

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.

Resources