728x90
Controller
package com.app.exam;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value = "/aaa", method = RequestMethod.GET)
public String aaa() {
if(true) throw new ArithmeticException("Arithmetic 예외처리 발생");
System.out.println("ex발생 후 진행 안 됨");
return "main";
}
@RequestMapping(value = "bbb", method = RequestMethod.GET)
public String bbb() {
if(true) throw new NullPointerException("NullPointer 예외처리 발생");
System.out.println("ex발생 후 진행 안 됨");
return "main";
}
@ExceptionHandler({ArithmeticException.class, NullPointerException.class})
public String errorPage() {
return "error";
}
}
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
error.jsp<br>
isErrorpage = "true" 설정<br>
<%= exception.getMessage() %><br>
<% out.print(exception.getMessage()); %>
</body>
</html>
===============================================
Bean으로 처리할 경우 --> servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<beans:property name="exceptionMappings">
<beans:props>
<beans:prop key="ArithmeticException">error</beans:prop>
<beans:prop key="NullPointerException">error</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
</beans:beans>
'단순 코드 기록 > Spring' 카테고리의 다른 글
Spring_Redirect_Forwarding (0) | 2024.02.14 |
---|---|
Spring_URLMAPPING (0) | 2024.02.14 |
Spring_returnType (0) | 2024.02.13 |
Spring_returnType (0) | 2024.02.13 |
Spring_Redirect (0) | 2024.02.13 |