2010-05-11 70 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 
     } 

     }