2010-05-11 71 views
6

默认情况下,将URL映射到控制器操作或视图时,Grails是区分大小写的。如何让我的URL映射不区分大小写?

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

我该怎么办(欢迎使用代码片段)使我的URL不区分大小写(即www.mywebsite.com/Book/list是一个有效的URL)?

+0

只是在想,你不能那么你的URL转换成小写使用它们之前?或者你不控制代码中的url? – 2010-05-11 09:52:17

+0

这就是要点!基本上问题是:如何在Grails中使用UrlMapping.groovy文件或其他内容来完成此操作。 – fabien7474 2010-05-11 10:22:20

回答

0

距离臀部拍,请尝试以下操作:

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

不工作:即使是恶魔,它也会抛出异常! – fabien7474 2010-05-11 11:11:56

0

控制器是一个“保留关键字”你必须将其重命名为类似“controllerName”

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

不,这给出:''不能在空对象上调用方法toLowerCase()。 – confile 2015-09-30 09:54:55

+0

你的意思是你不知道如何处理空行为?我没有说这不是一件容易的事 – 2015-10-07 18:26:24

0

亚伦桑德斯这是伟大的解决方案,但他的网站被阻止了我。这是他精彩的解决方案。确认它在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

这里是我的工作中,Grails 2.4基于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 
     } 

     }