【问题标题】:How to make my URL mapping case insensitive?如何使我的 URL 映射不区分大小写?
【发布时间】:2011-02-18 01:41:47
【问题描述】:

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

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

我能做些什么(欢迎使用代码 sn-ps)使我的 URL 不区分大小写(即 www.mywebsite.com/Book/list 是一个有效的 URL)?

【问题讨论】:

  • 只是在想,难道你不能在使用之前将你的网址转换成小写吗?或者您无法控制代码中的网址?
  • 这就是重点!基本上问题是:如何在 Grails 中使用 UrlMapping.groovy 文件或其他东西来做到这一点。

标签: grails url-routing


【解决方案1】:

这是我基于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 
        }

      }

【讨论】:

    【解决方案2】:

    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】:

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

      静态映射 = { "/$controllerName/$action?/$id?"{ 控制器 = {"${params.controllerName}".toLowerCase()} 动作 = action.toLowerCase() }

      【讨论】:

      • 不,这给出:Cannot invoke method toLowerCase() on null object
      • 您的意思是您不知道如何处理 null 的操作?我并没有说这是不费吹灰之力
      【解决方案4】:

      只是从臀部拍摄,尝试以下:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-13
        • 1970-01-01
        • 2019-10-04
        • 2017-07-19
        • 2011-10-14
        • 2017-01-27
        • 2012-05-25
        • 1970-01-01
        相关资源
        最近更新 更多