2010-05-11 11 views
6

डिफ़ॉल्ट रूप से Grails, नियंत्रक कार्यों या विचारों के लिए URL मैप करते समय केस-संवेदी है।मेरा यूआरएल मैपिंग केस असंवेदनशील कैसे बनाएं?

उदाहरण के लिए, www.mywebsite.com/book/list काम करेगा लेकिन www.mywebsite.com/Book/list एक 404 पृष्ठ वापस कर देगा।

मेरा यूआरएल केस-असंवेदनशील बनाने के लिए मैं क्या कर सकता हूं (कोड स्निपेट का स्वागत है) (यानी www.mywebsite.com/Book/list मान्य यूआरएल होने के नाते)?

+0

बस सोच, आप तो उन्हें प्रयोग करने से पहले लोअर केस में अपने यूआरएल परिवर्तित नहीं कर सकते आधारित पर Grails 2.4 काम करता है? या आप अपने कोड में यूआरएल के नियंत्रण में नहीं हैं? –

+0

यही बात है! असल में सवाल यह है कि: Gral में urlMapping.groovy फ़ाइल या कुछ और के साथ ऐसा कैसे करें। – fabien7474

उत्तर

0

कूल्हे से बस एक गोली मार, निम्न प्रयास करें:

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

काम नहीं कर रहा है: यहां तक ​​कि बदतर, यह एक अपवाद फेंकता है! – fabien7474

0

नियंत्रक एक "आरक्षित कीवर्ड" आप "controllerName"

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

नहीं, यह देता है: ''शून्य ऑब्जेक्ट' पर विधि को LowerCase() को नहीं बुला सकता है। – confile

+0

आपका मतलब है कि आप नहीं जानते कि कैसे एक क्रिया को संभालना है? मैंने यह नहीं कहा कि यह एक गैर-ब्रेनर है –

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

यहाँ कैसे मैं पर 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 
     } 

     } 
संबंधित मुद्दे