7.7. 책 오류 뷰 작성하기

책 정보가 없을 때 오류 페이지를 작성합니다. 오류 페이지는 컨트롤러에서 생성한 메시지를 보여주고, 컨트롤러에서 설정한 주소로 이동하게 됩니다.


패키지 익스플로어에서 src/main/resources > templates 아래에 common.error 폴더를 만든 후 422.html 파일을 생성합니다. 이 파일은 컨트롤러를 막론하고 422 오류가 생성될 경우 무조건 호출될 공통 오류(common error) 뷰 파일이 됩니다.

src/main/resources > templates.common.error > 422.html

<html xmlns:th="http://www.thymeleaf.org">  
  <head>  
  <title>Unprocessable Entity  
</title>  
  </head>  
  <body>  
    <script type="text/javascript" th:inline="javascript">  
    /*<![CDATA[*/  
    var message = /*[[${message}]]*/ 'message';   
    var loc = /*[[${location}]]*/ 'location';  
    /*]]>*/  
    alert(message);  
    location.href = loc;  
    </script>  
  </body>  
</html>  

이 코드가 html로 바뀌면 이렇게 보입니다.

<html>  
    <head>  
    <title>Unprocessable Entity  
  </title>  
    </head>  
    <body>  
      <script type="text/javascript">  
      /*<![CDATA[*/  
      var message = "\uCC45 \uC815\uBCF4\uAC00 \uC5C6\uC2B5\uB2C8\uB2E4.";   
      var loc = "\/book";  
      /*]]>*/     
      alert(message);  
      location.href = loc;  
      </script>  
    </body>  
  </html>