英文:
How to return custom error message in Spring Boot for /error page
问题
以下是翻译好的内容:
我有一个 Spring Boot 项目和一些使用 Thymeleaf 的 UI。我设计了一个 /error 页面来代替白屏级别的错误,它按预期工作。然而,我需要将一些字符串传递给 /error 并在错误页面中显示该字符串。我想知道如何做到这一点。
这是我的 /error 页面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Error Occurred.</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/styles.css}">
</head>
<body>
<th:block th:include="/_header"></th:block>
<th:block th:include="/menu"></th:block>
<div class="page-title">Error!</div>
<h3 style="color: red;">Sorry! Something went wrong !</h3>
</body>
</html>
Error 方法:
@RequestMapping("/error")
public String error() {
logger.info("Error Page called...");
return "error";
}
我想要发送一些特定的内容而不是错误消息 Sorry! Something went wrong !
,我想知道如何从调用者传递这些内容。如何做到这一点。
英文:
I have a spring boot project and some UI with thymeleaf. I have designed a /error page instead of the White level error, and it is working as expected. However I need to pass some string to /error and display that string in the error page. I am wondering how to do that.
This is my /error page :
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Error Occurred.</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/styles.css}">
</head>
<body>
<th:block th:include="/_header"></th:block>
<th:block th:include="/menu"></th:block>
<div class="page-title">Error!</div>
<h3 style="color: red;">Sorry! Something went wrong !</h3>
</body>
</html>
Error method :
@RequestMapping("/error")
public String error() {
logger.info("Error Page called...");
return "error";
}
Instead of the error message Sorry! Something went wrong !
I want to send something specific from the caller. How to do that.
答案1
得分: 1
你可以按照以下方式进行操作:
模板:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Error Occurred.</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/styles.css}">
</head>
<body>
<th:block th:include="/_header"></th:block>
<th:block th:include="/menu"></th:block>
<div class="page-title">Error!</div>
<h3 style="color: red;" th:text="${errorMsg}">Sorry! Something went wrong !</h3>
</body>
</html>
控制器:
@RequestMapping("/error")
public String error() {
logger.info("Error Page called...");
model.addAttribute("errorMsg", "Custom Error Message");
return "error";
}
英文:
You can do it this way
Template
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Error Occurred.</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/styles.css}">
</head>
<body>
<th:block th:include="/_header"></th:block>
<th:block th:include="/menu"></th:block>
<div class="page-title">Error!</div>
<h3 style="color: red;" th:text="${errorMsg}">Sorry! Something went wrong !</h3>
</body>
</html>
// Controller
@RequestMapping("/error")
public String error() {
logger.info("Error Page called...");
mmodel.addAttribute("errorMsg", "Custom Error Message");
return "error";
}
答案2
得分: 0
我最终找到了一个简单的方法,使用redirectAttributes.addFlashAttribute
。
以下是我某个控制器方法的代码,我在这个方法中重定向到/error
并附带原因:
String errorMsg = "购物车为空。请添加一些产品到购物车。";
CustomErrorMessage error = new CustomErrorMessage(errorMsg);
redirectAttributes.addFlashAttribute("errorForm", error);
return "redirect:/error";
以下是我的error
页面的代码:
<div class="page-title">
<h3 style="color: red">抱歉!出现了问题!</h3>
<th:block th:if="${errorForm == null}">
<h4>前往首页 : <a th:href="@{/}">首页</a></h4>
</th:block>
<th:block th:if="${errorForm != null}">
<div><ul><li>错误原因 : <span th:utext="${errorForm.errorMsg}"></span></li></ul></div>
<h4>前往首页 : <a th:href="@{/}">首页</a></h4>
</th:block>
</div>
英文:
I could find a simple way at last using redirectAttributes.addFlashAttribute
.
Here is my some controller method from where I am redirecting the /error
with reason :
String errorMsg = "Cart is Empty. Add some products to Cart." ;
CustomErrorMessage error = new CustomErrorMessage(errorMsg);
redirectAttributes.addFlashAttribute("errorForm", error);
return "redirect:/error";
Ans here is my error
page :
<div class="page-title">
<h3 style="color: red">Sorry! Something went wrong !</h3>
<th:block th:if="${errorForm == null}">
<h4>Go to Home Page : <a th:href="@{/}">Home</a></h4>
</th:block>
<th:block th:if="${errorForm != null}">
<div><ul><li>Error reason : <span th:utext="${errorForm.errorMsg}"></span></li></ul></div>
<h4>Go to Home Page : <a th:href="@{/}">Home</a></h4>
</th:block>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论