fab*_*474 14 grails url-mapping
获取HTTP状态代码500时,我想根据运行环境显示2个不同的页面.
在发展模式,我想显示stackStrace页(如默认Grails的500错误页面),并在生产模式,我想显示一个正式的"内部错误"页面.
有可能,我该怎么做?
tin*_*nny 19
您可以在其中执行特定于环境的映射 UrlMappings.groovy
grails.util.GrailsUtil 救援
它不漂亮,但我认为它将解决你的问题
例如
import grails.util.GrailsUtil
class UrlMappings {
static mappings = {
if(GrailsUtil.getEnvironment() == "development") {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/devIndex")
"500"(view:'/error')
}
if(GrailsUtil.getEnvironment() == "test") {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/testIndex")
"500"(view:'/error')
}
if(GrailsUtil.getEnvironment() == "production") {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/prodIndex")
"500"(view:'/error')
}
}
}
Run Code Online (Sandbox Code Playgroud)
Bur*_*ith 14
可能有一种更简洁的方法可以做到这一点,但我将错误代码映射到控制器并在那里处理逻辑:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?" { constraints {} }
"/"(view:"/index")
"403"(controller: "errors", action: "accessDenied")
"404"(controller: "errors", action: "notFound")
"405"(controller: "errors", action: "notAllowed")
"500"(view: '/error')
}
}
Run Code Online (Sandbox Code Playgroud)
然后创建相应的控制器(grails-app/conf/controllers/ErrorsController.groovy):
import grails.util.Environment
class ErrorsController extends AbstractController {
def accessDenied = {}
def notFound = {}
def notAllowed = {}
def serverError = {
if (Environment.current == Environment.DEVELOPMENT) {
render view: '/error'
}
else {
render view: '/errorProd'
}
}
}
Run Code Online (Sandbox Code Playgroud)
您需要在grails-app/views/errors(accessDenied.gsp,notFound.gsp等)以及新的grails-app/views/errorProd.gsp中创建相应的GSP.通过为所有错误代码路由到控制器方法,您可以更轻松地将逻辑添加到其他错误代码处理程序中.
| 归档时间: |
|
| 查看次数: |
7272 次 |
| 最近记录: |