728x90
반응형
1. ErrorPage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${statusCode} Error - Page not Found</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<style type="text/css">
body{
display:flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.error--message{
text-align: center;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="text-center">
<h1>${statusCode}</h1>
<p class="error-message">Page Not Found</p>
<p>${message}</p>
<a href="/index" class="btn btn-primary">Go To Home Page</a>
</div>
</div>
</body>
</html>
2. MainController.java
package com.tenco.bank.controller;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tenco.bank.handler.Exception.DataDeliveryException;
import com.tenco.bank.handler.Exception.RedirectException;
@Controller // IoC의 대상(싱글톤 패턴으로 관리된다.)
// IoC = inversion of controll (=제어의 역전)
public class MainController {
// REST API 기반으로 주소 설계 가능
// 주소 설계
// http://localhost:8080/main-page
@GetMapping({"/main-page", "/index"})
@ResponseBody
public String mainPage() {
System.out.println("mainPage() 호출 확인");
// [JSP 파일 찾기(yml)] - 뷰 리졸버
// prefix: /WEB-INF/view/
// main
// suffix: .jsp
// application.yml 에서 설정한 전위, 후위연산자를 통해 더욱 간단하게 사용이 가능하다.
return "main";
}
// todo - 삭제 예정
// 주소 설계
// http://localhost:8080/error-test1/true
// http://localhost:8080/error-test1/isError
@GetMapping("/error-test1")
public String errorPage() {
System.out.println("aaaaa");
if(true) {
throw new RedirectException("잘못된 요청입니다.", HttpStatus.NOT_FOUND);
}
return "main";
}
// http://localhost:8080/error-test2
@GetMapping("/error-test2")
public String errorData() {
System.out.println("bbbb");
if(true) {
throw new DataDeliveryException("잘못된 데이터입니다.", HttpStatus.BAD_REQUEST);
}
return "main";
}
}
3. HTTP 상태 코드란?
728x90
반응형
'Springboot' 카테고리의 다른 글
[SpringBoot] 10. DB 접근 기술 - MyBatis 설정 (0) | 2024.08.06 |
---|---|
[SpringBoot] 9. 어노테이션 정리 (0) | 2024.08.06 |
[SpringBoot] 7. Exception Handler 처리 (0) | 2024.08.05 |
[SpringBoot] 6. 메인컨트롤러와 메인페이지 구현하기 (0) | 2024.08.05 |
[SpringBoot] 5. 화면 구현 (0) | 2024.08.05 |