How to make my display code insensitive?

Grails, by default, is case sensitive when matching URLs with controller actions or views.

For example, www.mywebsite.com/book/list will work, but www.mywebsite.com/Book/list will return page 404.

What should I do (snippets of code are welcome) to make my URL case insensitive (i.e. www.mywebsite.com/Book/list to be a valid URL)?

+6
url-routing grails
source share
4 answers

Just shoot from the hip, try the following:

static mappings = { "/$controller/$action?/$id?"{ controller = controller.toLowerCase() action = action.toLowerCase() } 
0
source share

The controller is a "reserved keyword", you need to rename it to something like "controller_name"

  static mappings = {
 "/ $ controllerName / $ action? / $ id?" {
     controller = {"$ {params.controllerName}". toLowerCase ()}
     action = action.toLowerCase ()
 }
0
source share

Aaron Saunders has a great solution here, but his site has been blocked for me. Here is his fantastic solution. Confirmed that it works fine in Grails 2.x.

 import org.codehaus.groovy.grails.commons.GrailsClass class UrlMappings { def grailsApplication static mappings = { "/$controller/$action?/$id?"{ constraints { // apply constraints here } } //Case InSeNsItIvE URLS!!! "/$_ctrl/$_action/$id?" { controller = { def foundControllerMatch = false def returnCtrl = params._ctrl grailsApplication.controllerClasses.each { GrailsClass c -> def l_className = c.name.toLowerCase() // find the class if (params._ctrl?.equalsIgnoreCase(l_className) && foundControllerMatch == false) { foundControllerMatch = true returnCtrl = c.getLogicalPropertyName() //println " foundControllerMatch ${returnCtrl}" } } return returnCtrl } action = { def foundActionMatch = false def returnAction = params?._action def returnCtrl = params._ctrl grailsApplication.controllerClasses.each { GrailsClass c -> def l_className = c.name.toLowerCase() // find the class if (params._ctrl?.equalsIgnoreCase(l_className) && foundActionMatch == false) { returnCtrl = c.name c.URIs.each { _uri -> if (foundActionMatch == false) { def uri = _uri //println "u-> $uri" def uri_action uri_action = uri.split('/')[2] //println "uri_action-> $uri_action" if (uri_action?.trim()?.equalsIgnoreCase(params._action.trim())) { foundActionMatch = true returnAction = uri_action } } } } } return returnAction } } } } 
0
source share

This is how I work on Grails 2.4 based on http://www.clearlyinnovative.com/case-insensitive-url-mappings-in-grails

  "/$_ctrl/$_action/$id?" { controller = { def foundControllerMatch = false def returnCtrl = params._ctrl def grailsApplication = Holders.getGrailsApplication() grailsApplication.controllerClasses.each { GrailsClass c ->; def l_className = c.name.toLowerCase() // find the class if (params._ctrl?.equalsIgnoreCase(l_className) && foundControllerMatch == false) { foundControllerMatch = true returnCtrl = c.getLogicalPropertyName() // println " foundControllerMatch ${returnCtrl}" } } return returnCtrl } action = { def foundActionMatch = false def returnAction = params?._action def returnCtrl = params._ctrl def grailsApplication = Holders.getGrailsApplication() grailsApplication.controllerClasses.each { GrailsClass c ->; def l_className = c.name.toLowerCase() // find the class if (params._ctrl?.equalsIgnoreCase(l_className) && foundActionMatch == false) { returnCtrl = c.name c.URIs.each { _uri ->; if (foundActionMatch == false) { def uri = _uri // println "u-> $uri" def uri_action uri_action = uri.split('/')[2] // println "uri_action-> $uri_action" if (uri_action?.trim()?.equalsIgnoreCase(params._action.trim())) { foundActionMatch = true returnAction = uri_action } } } } } return returnAction } } 
0
source share

All Articles