Search code examples
error-handlingthymeleafhttp-status-code-404

How manage HTTPStatus with Thymeleaf switch in unique error page html


I created a custom controller to manage a different error:

@ControllerAdvice
public class MyErrorController {


    private static Logger logger = LoggerFactory.getLogger(ErrorController.class);

    @ExceptionHandler(UpdatableException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String indirizzoErrato(UpdatableException ex, final Model model) {
        logger.error("Indirizzo errato", ex);
        String errorMessage = (throwable != null ? ex.getMessage() : "Unknown error");
        model.addAttribute("errorMessage", errorMessage);
        return "error";
    }

    @ExceptionHandler(ReservedException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String erroreServer(ReservedException ex, final Model model) {
        logger.error("Errore Server", ex);
        String errorMessage = (ex != null ? ex.getMessage() : "Unknown error");
        model.addAttribute("errorMessage", errorMessage);
        return "error";
    }

}

But I'd like to see different message (and images) based on the error from the HTTPStatus response in the unique page html named error:

      <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Indirizzo Errato</title>
<head>
        <title>Errore</title>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="css/main.css" th:href="@{/css/main.css}" />
    </head>
    <body>
        <img class= "404" src="/images/NotFound.jpg">    
    </body>
</html>

I don't when I run my code, on console I have this message: "ERROR 10212 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat-7].[localhost] : Exception Processing ErrorPage[errorCode=0, location=/error]"

Please can you help me?

Thanks!


Solution

  • you can use custom error controller to show customize error message by your html page..

    here i give my code for your help you can return different error page base on statusCode value .... or sent same error page and set message into html page

    @Controller
    public class CustomErrorController implements ErrorController {
    
        @RequestMapping("/error")
        public String handleError(HttpServletRequest request, Model model) {
            Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
            Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
            model.addAttribute("statusCode",statusCode);
            if(statusCode==404){
                model.addAttribute("imgSrc","/img/fotFound.png");
            }else if(statusCode == 500){
                model.addAttribute("imgSrc","/img/internallError.png");
             }
            model.addAttribute("errorMessage","Error code : "+statusCode);
            return "error";
        }
    
        @Override
        public String getErrorPath() {
            return "/error";
        }
    }
    

    and change your html code like below

    <body>
        <img class= "404" th:src="${imgSrc}"> // set here your src which send form controller
    </body>