如何使我的URL映射不区分大小写?

时间:2010-05-11 09:48:28

标签: grails url-routing

默认情况下,Grails在将URL映射到控制器操作或视图时区分大小写。

例如,www.mywebsite.com / book / list即可使用但www.mywebsite.com/Book/list将返回404页面。

我可以做些什么(欢迎使用代码段)以使我的网址不区分大小写(例如www.mywebsite.com/Book/list是有效的网址)?

4 个答案:

答案 0 :(得分:0)

只是从臀部射击,尝试以下方法:

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

答案 1 :(得分:0)

Controller是一个“保留关键字”,您必须将其重命名为“controllerName”

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

答案 2 :(得分:0)

Aaron Saunders在这里有很好的解决方案,但是我的网站被封锁了。这是他梦幻般的解决方案。确认它在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

            }

        }
    }
}

答案 3 :(得分:0)

以下是我基于http://www.clearlyinnovative.com/case-insensitive-url-mappings-in-grails

在Grails 2.4上的工作方式
      "/$_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 
        }

      }