如何在Spring Boot中为/error页面返回自定义错误消息

huangapple go评论76阅读模式
英文:

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 :

&lt;!DOCTYPE html&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Error Occurred.&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{/css/styles.css}&quot;&gt;
&lt;/head&gt;

&lt;body&gt;    
	&lt;th:block th:include=&quot;/_header&quot;&gt;&lt;/th:block&gt;
	&lt;th:block th:include=&quot;/menu&quot;&gt;&lt;/th:block&gt;

	&lt;div class=&quot;page-title&quot;&gt;Error!&lt;/div&gt;
	&lt;h3 style=&quot;color: red;&quot;&gt;Sorry! Something went wrong !&lt;/h3&gt;    
&lt;/body&gt;
&lt;/html&gt;

Error method :

@RequestMapping(&quot;/error&quot;)
public String error() {
	logger.info(&quot;Error Page called...&quot;);
	return &quot;error&quot;;
}

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

    &lt;!DOCTYPE html&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Error Occurred.&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{/css/styles.css}&quot;&gt;
&lt;/head&gt;

&lt;body&gt;    
    &lt;th:block th:include=&quot;/_header&quot;&gt;&lt;/th:block&gt;
    &lt;th:block th:include=&quot;/menu&quot;&gt;&lt;/th:block&gt;

    &lt;div class=&quot;page-title&quot;&gt;Error!&lt;/div&gt;
    &lt;h3 style=&quot;color: red;&quot; th:text=&quot;${errorMsg}&quot;&gt;Sorry! Something went wrong !&lt;/h3&gt;    
&lt;/body&gt;
&lt;/html&gt;



//  Controller
 @RequestMapping(&quot;/error&quot;)
public String error() {
    logger.info(&quot;Error Page called...&quot;);
    mmodel.addAttribute(&quot;errorMsg&quot;, &quot;Custom Error Message&quot;);
    return &quot;error&quot;;

}

答案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 = &quot;Cart is Empty. Add some products to Cart.&quot; ;
CustomErrorMessage error = new CustomErrorMessage(errorMsg);
redirectAttributes.addFlashAttribute(&quot;errorForm&quot;, error);
return &quot;redirect:/error&quot;;

Ans here is my error page :

    &lt;div class=&quot;page-title&quot;&gt;
		&lt;h3 style=&quot;color: red&quot;&gt;Sorry! Something went wrong !&lt;/h3&gt;
		&lt;th:block th:if=&quot;${errorForm == null}&quot;&gt;
			&lt;h4&gt;Go to Home Page : &lt;a th:href=&quot;@{/}&quot;&gt;Home&lt;/a&gt;&lt;/h4&gt;
		&lt;/th:block&gt;
		&lt;th:block th:if=&quot;${errorForm != null}&quot;&gt;
			&lt;div&gt;&lt;ul&gt;&lt;li&gt;Error reason : &lt;span th:utext=&quot;${errorForm.errorMsg}&quot;&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
			&lt;h4&gt;Go to Home Page : &lt;a th:href=&quot;@{/}&quot;&gt;Home&lt;/a&gt;&lt;/h4&gt;    			
		&lt;/th:block&gt;
	&lt;/div&gt;

huangapple
  • 本文由 发表于 2020年9月25日 18:51:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64062675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定