如何使我的URL映射不区分大小写? [英] How to make my URL mapping case insensitive?

查看:133
本文介绍了如何使我的URL映射不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



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

解决方案

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

 / $ _ ctrl / $ _ action / $ id? {
controller = {
def foundControllerMatch = false
def returnCtrl = params._ctrl
$ b $ def grailsApplication = Holders.getGrailsApplication()


grailsApplication.controllerClasses.each {GrailsClass c - >;
def l_className = c.name.toLowerCase()

//找到类
if(params._ctrl?.equalsIgnoreCase(l_className)&& foundControllerMatch == false ){
foundControllerMatch = true
returnCtrl = c.getLogicalPropertyName()
// printlnfoundControllerMatch $ {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()

//找到类
if(params._ctrl?.equalsIgnoreCase(l_className)&& foundActionMatch == false ){

returnCtrl = c.name

c.URIs.each {_uri - > ;;

if(foundActionMatch == false){
def uri = _uri

// printlnu-& gt; $ uri

def uri_action
uri_action = uri.split('/')[2]

// printlnuri_action-& gt; $ uri_action
if(uri_action ?.trim()?。equalsIgnoreCase(params._action.trim())){
foundActionMatch = true
returnAction = uri_action
}
}
}


return returnAction


$ b code $ pre

Grails, by default, is case-sensitive when mapping URL to controller actions or views.

For instance, www.mywebsite.com/book/list will work BUT www.mywebsite.com/Book/list will return a 404 page.

What can I do (code snippets are welcomed) to make my URL case-insensitive (i.e. www.mywebsite.com/Book/list being a valid url) ?

解决方案

Here is how I works on Grails 2.4 based on 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 
        }

      }

这篇关于如何使我的URL映射不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆