gpt4 book ai didi

grails - 如何映射urlmappings文件中的500错误?

转载 作者:行者123 更新时间:2023-12-02 14:48:29 25 4
gpt4 key购买 nike

为了测试urlmappings文件中的映射500状态。目的是显示自定义错误 View 页面。我有意抛出异常来测试这一点。

我已经设置了这个简单的代码。

import java.nio.file.AccessDeniedException

class HomeController {

def index() {

throw new AccessDeniedException('asd')
}
}

我在URL映射中映射500个代码
package exceptiontest
import java.nio.file.AccessDeniedException

class UrlMappings {

static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}

"/"(controller: 'home', action: 'index')
"404"(view:'/notFound')

"500"(controller: "errors", action: "error403")

}
}

这是错误 Controller
package exceptiontest

class ErrorsController {

def error403() {}

}

这是error403.gsp文件
<html>
<head>
<title>Access denied!</title>
<meta name='layout' content='main' />
</head>

<body>
<h1>Access Denied</h1>
<p>We're sorry, but you are not authorized
to perform the requested operation.</p>
</body>
</html>

它不会显示此错误 View 。我想让此urlmappings用于500个代码吗?感谢您的帮助。谢谢!

我正在使用Grails 3.3.8。

最佳答案

您收到500错误还是403错误?尝试在URL映射中添加403,或在 Controller 中尝试以下操作,以获取500错误。

导入静态org.springframework.http.HttpStatus。*

呈现状态:INTERNAL_SERVER_ERROR

关于grails - 如何映射urlmappings文件中的500错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52492393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com