Grails 3.0.1 此处。我正在寻求完成一个特定的 URL/ Controller 结构。我的应用程序部署在根上下文 ( /
),这意味着它在本地运行为 http://localhost:8080
,非本地为 http://someserver.example.org
.
我想要 /app/*
下的所有内容经过身份验证并被视为“核心应用程序”的一部分(需要登录)。该 URL 之外的任何内容都被视为公共(public)网站的一部分(未经身份验证)。不过,我想要/app/
本身只是某种占位符;我不希望它成为一个 Grails Controller 。因此:
-
http://localhost:8080/app
可以配置(UrlMappings
?)以显示登录页面 -
http://localhost:8080/app/<controller>/<action>
遵循典型的 Grails Controller /操作套装,并且会调用正确的 Controller 操作
因此http://localhost:8080/app/order/create
将进行身份验证,如果登录,则调用 OrderController#create
操作,可能会呈现 createOrder.gsp
.
我想知道 Grails 3.x 的方法是:
- 允许
/app/
存在,但不是作为 Controller (就像我说的,也许只是重定向/映射到登录页面) - 允许下面的任何内容
/app/
遵循 Controller /操作范例
关于如何实现这一点的想法?
更新
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/app/$controller/$action?/$id?" {
???
}
"/"(view:"/index")
"500"(view:'/error')
"404"(view:'/notFound')
}
}
最佳答案
Grails 可以为 Controller 声明 namespace 。有了这个,您可以将所有 Controller 放在 namespace “app”下,这应该会导致您的第二个问题。请参阅docs了解更多详情。
然后使用正常的 Spring 安全设置(@Secured
例如)来完成安全限制。
关于Grails Controller 和 URL 映射魔法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29952281/