- 컨트롤러 작업 중 에러가 발생할 때 처리하는 방법
- @ControllerAdvice : 컨트롤러 작업 중 에러가 발생할 때 처리하는 클래스
- @ExceptionHandler(Exception.class) : 예외를 처리할 메서드
- @ResponseStatus(HttpStatus.NOT_FOUND) : 404에러를 처리하는 메서드
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class Ex07Controller {
@RequestMapping(value="/ex07.do", method=RequestMethod.GET)
public String ex07(@RequestParam(value="num", defaultValue = "10") int num, Model model) {
//ex07.do?num=100
model.addAttribute("result", 100 / num);
return "ex07";
}
}
package com.test.exception;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;
import lombok.extern.log4j.Log4j;
@ControllerAdvice //컨트롤러 작업 중 에러가 발생하면 이 클래스를 호출
@Log4j
public class CommonExceptionAdvice {
@ExceptionHandler(Exception.class)
public String except(Exception e, Model model) {
log.error("예외 발생");
model.addAttribute("code", "A001");
model.addAttribute("e", e);
return "error"; //error.jsp 페이지 호출
}
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String notfound( NoHandlerFoundException e) {
return "notfound";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>error.jsp</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<h1>에러 페이지</h1>
<div>
<div>사용에 불편을 드려 죄송합니다...</div>
<div>${code}</div>
<div>${e.getMessage()}</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>notfound.jsp</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<h1>Page not found.</h1>
<div>
URL이 잘못됐거나, 서버가 일시적으로 불안정합니다.
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
'서버 > Spring' 카테고리의 다른 글
[스프링(Spring)] 파일 업로드 / 다운로드 (0) | 2023.06.20 |
---|---|
[스프링(Spring)] MyBatis (1) | 2023.06.16 |
[스프링(Spring)] MVC 데이터 수신 및 전송 (1) | 2023.06.15 |
[스프링(Spring)] AOP (1) | 2023.06.15 |
[스프링(Spring)] JUnit (0) | 2023.06.14 |