Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

KSI일기장

0427spring예외처리 본문

Spring

0427spring예외처리

MyDiaryYo 2023. 4. 27. 15:36

//error.jsp

<%@ 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 lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Error</title>
    <style>
        #error-container{
            width: 800px;
            height: 300px;
            text-align: center;
            
            position: absolute;
            top : 0; bottom: 0; left: 0; right: 0;
            margin: auto;
        }
        #error-container > h1{ margin-bottom: 50px; }

        .error-cnotent-title{
        	text-align: left;
            font-weight: bold;
        }
        #btn-area{ text-align: center;  }
    </style>
</head>
<body>
    <div id="error-container">
        <h1>${requestScope.errorMessage}</h1>
        
        <span class="error-cnotent-title"> 발생한 예외 : ${e}</span>
        <p>
        	자세한 문제 원인은 이클립스 콘솔을 확인해주세요.
        </p>
        
        <div id="btn-area">
        	<a href="${pageContext.servletContext.contextPath}">메인 페이지</a>
        	
        	<button onclick="history.back()">뒤로 가기</button>
        </div>
    </div>
</body>
</html>

***예외처리***
-스프링 예외처리 방법
 * 1순위 :메서드 별로 예외처리(try~catch /throws)
 * 2순위 :하나의 컨트롤러에서 발생하는 예외를 모아서 처리
 *  ->@ExceptionHandler(메서드에서 작성)
 * 3순위 :전역(웹 애플리케이션)에서 발생하는 예외를 모아서 처리
 *   ->@ControllerAdvice(클래스에서 작성)
 *   ->에러 처리용 클래스 따로 만들어서 처리
 * 
**스프링 예외처리 방법 2순위**
멤버 컨트롤러에서 발생하는 모든 예외를 모아서 처리
 @ExceptionHandler(Exception.class)
 //->@ExceptionHandler(예외종류.class)
 public String exceptionHandler(Exception e, Model model) {
 e.printStackTrace();
 model.addAttribute("errorMassage", "서비스 이용 중 문제 발생");
 model.addAttribute("e", e);
return "common/error";
 }

 

 

**스프링 예외처리 방법 2순위**

//ExceptionController

package edu.kh.comm.main.controller;

import java.sql.SQLException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class ExceptionController {

	
	//스프링 예외처리 방법 2순위
	//멤버 컨트롤러에서 발생하는 모든 예외를 모아서 처리
	@ExceptionHandler(Exception.class)
	//->@ExceptionHandler(예외종류.class)
	public String exceptionHandler(Exception e, Model model) {
		e.printStackTrace();
		
		model.addAttribute("errorMassage", "서비스 이용 중 문제 발생");
		model.addAttribute("e", e);
		
		return "common/error";
	}

	@ExceptionHandler(SQLException.class)
	//->@ExceptionHandler(예외종류.class)
	public String sqlExceptionHandler(SQLException e, Model model) {
		e.printStackTrace();
		
		model.addAttribute("errorMassage", "서비스 이용 중 SQLException 발생");
		model.addAttribute("e", e);
		
		return "common/error";
	}
	
}

 

-테스트를 위한 강제 예외 만들기

 

로그인 시도하면 ;때문에 에러가 발생할 것입니다

 

-->

error.jsp 창이 띄워졌습니다