Web Application MessageResource

■ Spring에서 직접 Exception을 처리하지 않고 그 Exception을 WebApplicationContext로 던지고 web.xml에서 지정한 404 에러 페이지에서 처리하도록 하는 경우가 있다. 만약 그 404 에러 페이지에서 MessageResource를 처리하는 코드가 있다면 Spirng에서 등록한 MessageResource를 쓰지 못하고 별도로 등록해야 한다. 즉 Spring DispatcherServlet에서 처리하지 않는(처리범위 내에 있지 않은) MessageResource를 등록한는 방법을 알아본다.
■ web.xml에 등록된 contextConfigLocation(application-config.xml)

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/application-config.xml</param-value>
</context-param>

 

■ application-config.xml에 messageSource를 아래와 같이 등록한다.

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
  <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="messages.application" /> 
  </bean>
</beans>

 

■ application-config.xml에 messageSource의 value를 ‘messages.application’으로 등록했기 때문에 아래의 캡쳐화면처럼 messages디렉토리 아래에 application.properties, application_ko.properties, application_en.properties등을 생성한다. 이 properties파일에 등록한 메세지들은 web.xml의 예외페이지에서 MessageResource를 사용할 수 있게 한다.
2016-03-08 18;41;04

 
 
 

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다