How to make some URL mappings depending on the environment? - grails

When getting an HTTP status code 500, I want to display 2 different pages according to the running environment.
In development mode, I want to display a stackStrace page (like the default Grails 500 error page) and in production mode, I want to display a formal "internal error" page.
Is it possible and how can I do that ?

You can do environment specific mappings within UrlMappings.groovy
grails.util.GrailsUtil to the rescue
Its not pretty, but I think it will solve your issue
E.g
import grails.util.GrailsUtil
class UrlMappings {
static mappings = {
if(GrailsUtil.getEnvironment() == "development") {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/devIndex")
"500"(view:'/error')
}
if(GrailsUtil.getEnvironment() == "test") {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/testIndex")
"500"(view:'/error')
}
if(GrailsUtil.getEnvironment() == "production") {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/prodIndex")
"500"(view:'/error')
}
}
}

There may be a cleaner way to do this, but I'd got with mapping the error code to a controller and handling the logic there:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?" { constraints {} }
"/"(view:"/index")
"403"(controller: "errors", action: "accessDenied")
"404"(controller: "errors", action: "notFound")
"405"(controller: "errors", action: "notAllowed")
"500"(view: '/error')
}
}
and then create the corresponding controller (grails-app/conf/controllers/ErrorsController.groovy):
import grails.util.Environment
class ErrorsController extends AbstractController {
def accessDenied = {}
def notFound = {}
def notAllowed = {}
def serverError = {
if (Environment.current == Environment.DEVELOPMENT) {
render view: '/error'
}
else {
render view: '/errorProd'
}
}
}
You'll need to create the corresponding GSPs in grails-app/views/errors (accessDenied.gsp, notFound.gsp, etc.) and also the new grails-app/views/errorProd.gsp. By routing to a controller method for all error codes you make it easier to add logic to the other error code handlers in the future.

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")

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.

Grails URL Mapping question

I'm using Grails 1.2.1. I want to set up this mapping ...
http://localhost:8080/context-path/mediaproxy
So I added this to my URLMappings.groovy file ...
class UrlMappings {
static mappings = {
‰name mediaproxy: "/mediaproxy" {
controller = "SocialMediaCacheProxy"
action = "index"
}
"/"(view:"/index")
"500"(view:'/error')
}
}
However, I'm getting a 404 when I visit the above URL. Here is how I set up my controller
class SocialMediaCacheProxyController {
def index = {
if (params.dumpAll != null) {
} else if (params.url != null) {
doCacheTransport(params, response);
} // if
}
...
}
Any ideas what I'm doing wrong? Thanks, - Dave
There are is some weird character in front of your named mapping (‰) and your controller name should be lowercase on the first character so that it points to SocialMediaCacheProxyController.
If you don't need a named mapping the following mapping would do the trick for you:
class UrlMappings {
static mappings = {
"/mediaproxy"(controller:"socialMediaCacheProxy", action:"index")
"/"(view:"/index")
"500"(view:'/error')
}
}
It might be some problem with your question formatting but I would expect the url mapping to look like this:
class UrlMappings {
static mappings = {
"/mediaproxy" {
controller = "SocialMediaCacheProxy"
action = "index"
}
"/"(view:"/index")
"500"(view:'/error')
}
}

Weird UrlMappings behavior for Grails 1.2.1

I've created a Grails (1.2.1) application in SpringSource Tools Suite 2.3.2 and here is my UrlMappings.groovy:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/broadcasters/setInterval" { //cause a 404
controller = "broadcaster"
action = "setRefreshInterval"
}
"/broadcasters/online/$id?" { //this one is OK
controller = "broadcaster"
action = "listOnlineBroadcasters"
}
"/broadcasters/$id?" { //this one is OK
controller = "broadcaster"
action = "listAllBroadcasters"
}
"/" (controller: "login", action:"auth")
"/logout" (controller: "logout")
"500"(view:'/error')
"404"(view:'/404')
}
}
Here is my controller
package xxx.yyy.controllers
import org.codehaus.groovy.grails.plugins.springsecurity.Secured
#Secured(['ROLE_ADMIN'])
class BroadcasterController {
def broadcasterService
static defaultAction = "listAllBroadcasters"
def listOnlineBroadcasters = {
...
}
def listAllBroadcasters = {
...
}
def setRefreshInterval = {
...
}
}
When I access the url /broadcasters/setInterval, I've got a 404 both as normal or ajax request. I also write a simple unit test to check the for my UrlMappings:
class GSMUrlMappingTests extends GrailsUrlMappingsTestCase {
void testUrlMapping() {
assertUrlMapping ("/broadcasters/setInterval", controller: "broadcaster", action: "setRefreshInterval")
}
}
And the test failed! Is it a bug of Grails 1.2.1 or am I missing something?
Here is the plugins I've used
plugins.acegi=0.5.2
plugins.debug=1.0.2
plugins.hibernate=1.2.1
plugins.jdbc-pool=0.1
plugins.tomcat=1.2.1
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
is closed.
Other mappings need to be within

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