How to make my URL mapping case insensitive?

前端 未结 4 1157
慢半拍i
慢半拍i 2021-01-05 14:39

Grails, by default, is case-sensitive when mapping URL to controller actions or views.

For instance, www.mywebsite.com/book/list will work BUT www.mywebsite.com/Boo

相关标签:
4条回答
  • 2021-01-05 14:59

    Here is how I works 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 讨论(0)
  • 2021-01-05 15:15

    Controller is a "reserved keyword" you have to rename it to something like "controllerName"

    static mappings = {
    "/$controllerName/$action?/$id?"{
        controller = {"${params.controllerName}".toLowerCase()}
        action = action.toLowerCase()
    }
    
    0 讨论(0)
  • 2021-01-05 15:16

    Just a shoot from the hip, try the following:

    static mappings = {
    "/$controller/$action?/$id?"{
        controller = controller.toLowerCase()
        action = action.toLowerCase()
    }
    
    0 讨论(0)
  • 2021-01-05 15:16

    Aaron Saunders has as great solution here, but his site was blocked for me. Here is his fantastic solution. Confirmed it works great 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 讨论(0)
提交回复
热议问题