spring exception – SimpleMappingExceptionResolver

참고 – http://www.journaldev.com/2651/spring-mvc-exception-handling-exceptionhandler-controlleradvice-handlerexceptionresolver-json-response-example

spring exception 파일
■ web.xml

<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<error-page>
  <error-code>404</error-code>
  <!--<location>/error/404code.jsp</location>-->
  <location>/WEB-INF/view/exception/throwable.jsp</location>
</error-page>
<error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/error/404code</location>
</error-page>  

■ spring-config.xml

<import resource="exception-config.xml" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
  <property name="prefix" value="/WEB-INF/view/"/>
  <property name="suffix" value=".jsp"/>
</bean>

■ exception-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
  ">

  <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
      <props>
        <prop key="java.lang.Exception">exception/defaultException</prop>
        <prop key="RuntimeException">exception/runtimeException</prop>
        <prop key="java.sql.SQLException">exception/sqlException</prop>
      </props>
    </property>
  </bean>
</beans>

■ HelloController.java

@RequestMapping(value="/hello")
public class HelloController {

	private static final Logger log = LoggerFactory.getLogger(HelloController.class);
	
	@RequestMapping(value="/list")
	public String list (
			HttpServletRequest request,
			ModelMap model) throws Exception {		
		return "/hello/list";
	}
	
	@RequestMapping(value="/list2")
	public String list2 (
			HttpServletRequest request,
			ModelMap model) throws Exception {
		throw new Exception();
	}
	
	@RequestMapping(value="/list3")
	public String list3 (
			HttpServletRequest request,
			ModelMap model) throws Exception {
		throw new SQLException();
	}
	
	@RequestMapping(value="/list4")
	public String list4 (
			HttpServletRequest request,
			ModelMap model) throws Exception {
		throw new RuntimeException();
	}
}

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다