Grails 如何使我的URL映射不区分大小写?
默认情况下,Grails在将URL映射到控制器操作或视图时区分大小写 例如,www.mywebsite.com/book/list将起作用,但www.mywebsite.com/book/list将返回404页Grails 如何使我的URL映射不区分大小写?,grails,url-routing,Grails,Url Routing,默认情况下,Grails在将URL映射到控制器操作或视图时区分大小写 例如,www.mywebsite.com/book/list将起作用,但www.mywebsite.com/book/list将返回404页 我能做些什么(欢迎使用代码片段)使我的URL不区分大小写(即www.mywebsite.com/Book/list是一个有效的URL)?只需简单介绍一下,请尝试以下操作: static mappings = { "/$controller/$action?/$id?"{ cont
我能做些什么(欢迎使用代码片段)使我的URL不区分大小写(即www.mywebsite.com/Book/list是一个有效的URL)?只需简单介绍一下,请尝试以下操作:
static mappings = {
"/$controller/$action?/$id?"{
controller = controller.toLowerCase()
action = action.toLowerCase()
}
控制器是一个“保留关键字”,您必须将其重命名为类似“controllerName”的名称
静态映射={
“/$controllerName/$action?/$id?”{
控制器={“${params.controllerName}”.toLowerCase()}
action=action.toLowerCase()
}
Aaron Saunders在这里有一个很好的解决方案,但他的网站被我屏蔽了。这是他的奇妙解决方案。证实了它在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
}
}
}
}
下面是我如何基于
想想看,你不能在使用URL之前将其转换成小写吗?或者你不能控制代码中的URL?这就是问题所在!基本上问题是:如何在Grails中使用UrlMapping.groovy文件或其他文件来实现这一点。不,这会给出:
无法调用方法toLowerCase()在空对象上
。你是说你不知道如何处理一个空的动作?我没说这是一个无脑的动作
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
}
}
}
}
"/$_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
}
}