note

Spring 예외처리 본문

JSP/Spring

Spring 예외처리

투한 2012. 3. 6. 11:57






















dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">


	<!-- 예외 처리 -->
	<bean class="org.springframwork.web.servlethandler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.ArithmeticException">
					error/mathException
				</prop>
				<prop key="java.lang.Exception">
					error/exception
				</prop>
			</props>
		</property>
	</bean>
	<bean class="madvirus.spring.chap06.controller.ArithmeticOperatorController" />
	<!-- View 글로벌 설정 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 리소스 번들 지정 -->
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
	 <property name="basenames">
	 	<list>
	 		<value>messages.validation</value>
	 	</list>
	 </property>
	</bean>
</beans>




ArithmeticOperatorController.java
package madvirus.spring.chap06.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ArithmeticOperatorController {

	@RequestMapping("/math/divide.do")
	public String divide(
			@RequestParam("op1") int op1,
			@RequestParam("op2") int op2, Model model) {
		

		model.addAttribute("result",op1/op2);

		return "math/result";
	}
}




math/result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>연산 성공</title>
</head>
<body>
연산 성공
</body>
</html>



aa
aaa