Grails urlMappings action and controller behavior not using controller logic - grails

I have a grails app where the content of urlMappings.groovy file is:
"/" {
controller="home"
view="home"
}
My home controller looks like
class HomeController {
List products = [
['price': 100, 'desc':'a'],
['price': 200, 'desc':'b']
]
def home() {
[products: products]
}
When I navigate to localhost:8080/myProject/home/home, I have access to "${products}", but when I navigate to localhost:8080/myProject, "${products}" statement is null.
Why is this and how can I make the localhost:8080/myProject act the same way as localhost:8080/myProject/home/home?

You should do
"/"(controller: 'home', action: 'home')

Related

UrlMapping to Index is not working

I am trying to get my Index which in my case is just localhost:8080 to go to a different page. This code is not working and I'm just going to the index page instead of rendering "this worked"
My UrlMappings file looks like this:
package appworld
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/" {
controller = "Home"
action = "isUserLoggedIn"
}
"500"(view:'/error')
"404"(view:'/notFound')
}
}
The method in HomeController.groovy looks like:
def isUserLoggedIn() {
println("We made it from index")
render "this worked"
}
It looks like your syntax is off. Try:
"/"(controller:"home", action:"isUserLoggedIn")

springsecurity successHandler deafultTargetUrl does not work in grails

my successHandler deafultTargetUrl is this
grails.plugin.springsecurity.successHandler.deafultTargetUrl = '/home'
home set with permitAll
'/home': ['permitAll']
but when i submit form then home page does not open.
i am try also this
grails.plugin.springsecurity.successHandler.alwaysUseDefault = true
grails.plugin.springsecurity.successHandler.deafultTargetUrl = '/home'
home controller code is
class HomeController {
def index() {
render(view:'welcome')
}
}
deafultTargetUrl should be defaultTargetUrl.

springSecurity.denied.message in grails 2.2.2

i am trying to Different home page for user depending upon its Role
grails.plugins.springsecurity.successHandler.defaultTargetUrl = "/home"
grails.plugins.springsecurity.securityConfigType="InterceptUrlMap"
grails.plugins.springsecurity.interceptUrlMap=[
....
....
'/User/**':['ROLE_USER'],
'/home/**':['ROLE_ADMIN','ROLE_USER'],
....
....
]
i set success handler controller "HomeController"
in that i redirect role wise home page
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
class HomeController {
def index() {
if (SpringSecurityUtils.ifAllGranted('ROLE_ADMIN')) {
redirect controller: '...', action: '...'
return
}
if (SpringSecurityUtils.ifAllGranted('ROLE_USER')) {
redirect controller: 'user', action: 'show'
return
}
}
}
In this When i logged in through ADMIN Profile it hits "HomeController" and redirect as well
But When I trying to Log In from User Profile it gives me an error springSecurity.denied.message...
The problem seems to be with your redirect call:
redirect controller: 'UserController'
it should be
redirect controller: 'user'
Because in this cases you follow the url convension: :8080/your-app/user/show
'/user/**':['ROLE_USER'],
instead
'/User/**':['ROLE_USER'],

UrlMapping causing method not to render different view than default

class SearchController {
def list = {
List<Product> productsList = productRepository.findProductBySearchPhrase(params.searchPhrase)
render(view: "/product/list", model: [products: productsList])
}
}
class UrlMappings {
"/$controller/$action?/$id?" {
constraints {}
}
"/search" {
controller = "search"
view = "list"
constraints {}
}
}
1) This URL works properly, rendering GSP from /views/product/list directory.
myapp.com/search/list?searchPhrase=underware
2) This URL doesn't do the work, rendering /views/search/list.
myapp.com/search?searchPhrase=underware
Any ideas?
May be you want to replace 'view' with 'action' in the search URL Mapping.

Best practices for grails index page

What is the right way to populate the model for the index page in a grails app? There is no IndexController by default, is there some other mechanism for getting lists of this and that into the model?
I won't claim that this is the right way, but it is one way to start things off. It doesn't take much to have a controller be the default. Add a mapping to UrlMappings.groovy:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"500"(view:'/error')
"/"
{
controller = "quote"
}
}
}
Then add an index action to the now default controller:
class QuoteController {
def index = {
...
}
}
If what you want to load is already part of another action simply redirect:
def index = {
redirect(action: random)
}
Or to really get some reuse going, put the logic in a service:
class QuoteController {
def quoteService
def index = {
redirect(action: random)
}
def random = {
def randomQuote = quoteService.getRandomQuote()
[ quote : randomQuote ]
}
}
I couldn't get Ed T's example above to work. Perhaps Grails has changed since then?
After some experimentation and some rummaging on the net, I ended up with this in UrlMappings.groovy:
"/"(controller: 'home', action: 'index')
My HomeController looks like this:
class HomeController {
def index = {
def quotes = = latest(Quote.list(), 5)
["quotes": quotes, "totalQuotes": Quote.count()]
}
}
And in views/home, I have an index.gsp file. That makes the index.gsp file in views unnecessary, so I removed it.
The good answer: If you need to populate a model for the index page, it's time to change from using a straight index.gsp to an index controller.
The evil answer: If you create a filter whose controller is '*', it'll get executed even for static pages.
In grails 1.3.6 for just adding
"/index.gsp"(uri:"/")
to UrlMappings.groovy worked fine for me. It has the same effect as adding a new controller and mappings like described before.
Below is my complete UrlMappings.groovy:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
"/index.gsp"(uri:"/")
}
}

Resources