1

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.

2
  • You don't really normally call the error handler. Can you explain in more detail what you're trying to do? (Note that Spring Boot already has some magic support for providing your own templates; see BasicErrorController for the details.) Commented Sep 25, 2020 at 11:35
  • @chrylis-cautiouslyoptimistic- My basic requirement is whenever the error page is called due to some failures in some api I want to pass the reason of failure and print it on my page. Just a string. Thats it. Commented Sep 25, 2020 at 12:50

2 Answers 2

0

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";

}
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.