我想包含 Grails 的默认 url 映射。也就是说,如果请求的 url 映射中没有规则,则应重定向到 home/index。
我当前的 url 映射是:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?" {
constraints {
// apply constraints here
}
}
"/"(controller:"home", action:"index")
"/admin"(controller:"login",action:"authadmin")
"/robots.txt"(controller:"home",action:"robots")
"/sitemap.xml"(controller:"home",action:"sitemap")
"404"(controller:"home",action:"notfound")
"500"(view:'/error')
"/**"(controller:"home", action:"index")
}
}
我使用最后一个以/** 开头的规则来映射所有在 url 映射中没有特定规则的传入请求。问题是当我将以下内容放入 GSP 文件中时
<g:link controller="home" action="index"><div class="MainLogo"></div></g:link>
然后网址如下所示:
http://www.example.com/**
有办法纠正这个问题吗?如何在 url 映射中未定义规则的情况下为 url 创建默认映射?
最佳答案
我不能说我真的建议这种方法,因为实际上最好为不存在的页面发送 404,但无论如何我认为解决此问题的最佳方法是双通配符路径。请参阅:
http://grails.org/doc/latest/guide/theWebLayer.html#mappingWildcards
例如:
"/$path**"(controller:"home", action:"index")
关于java - 如何在 Grails UrlMappings 中设置默认 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24576484/