本文介绍了Play Framework 2.2.1-不区分大小写的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Play的新手,目前正在使用Play 2.2.1

I am novice to the Play and currently working Play 2.2.1

我正在尝试为路由"中定义的端点实现不区分大小写的路由

I am trying to acheive case insensitive routing for my endpoints which are defined in "routes"

例如我在路由文件中定义了一条说/accessLicense的路由,如下所示

e.g. I have define an route say /accessLicense in routes file, it would look like below

GET /accessLicense controller.MyController.accessLicense()

现在,如果我启动/accessLicense,它会很棒;如预期的那样,但是如果尝试使用/AccessLicense,/AcCeSSLicenSe或拼写完全相同的单词的大写/小写字母的任何其他组合,则将无效.

Now, if I fire /accessLicense it woks great; as expected, but if try to fir /AccessLicense, /AcCeSSLicenSe or any other combination of upper/lower case letter which spell exact same word, it doesn't work.

在此先感谢您的指导和支持!

Thanks in advance for guidance and support!!!

推荐答案

不幸的是,AFAIK没有办法神奇地打开可以实现您想要的功能的开关.值得庆幸的是,有一个变通方法,IMHO较差,但它是可以做到的最好的方法.

Unfortunately, AFAIK, there is no way to magically turn on a switch that will do what you want. Thankfully, there's a workaround, inferior IMHO but its the best that can be done.

GET/[aA] [cC] [cC] [eE] [sS] [sS] .....

GET /[aA][cC][cC][eE][sS][sS].....

我做了以下事情,这与我只将URL的第一部分小写的特定要求相匹配.因此,GET/AbCdE/XyZ将变为GET/abcde/XyZ,如果该路由中有操作,则将对其进行适当处理.

I did the following, which matches my specific requirement of lower-casing only the first part of URL. So GET /AbCdE/XyZ will become GET /abcde/XyZ and if this has an action in the routes than it will be handled appropriately.

override def onRouteRequest( request: RequestHeader ) = {
   val path = request.path
   val split = path.split( "/" ).toList

   val lowerCasePath = split match{
     case ""::Nil  => ""::Nil
     case ""::x::y => ""::x.toLowerCase::y
   }

   logger.error( lowerCasePath.toString )

   super.onRouteRequest( request.copy( path = lowerCasePath.mkString( "/" ) ) )
}

EDIT参见此处: https://jazzy.id.au/2013/05/08/advanced_routing_in_play_framework.html

EDIT See here: https://jazzy.id.au/2013/05/08/advanced_routing_in_play_framework.html

这篇关于Play Framework 2.2.1-不区分大小写的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:05