[spring-config.xml]
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:interceptors> <!-- 인터셉터로 사용할 빈을 정의한다. -->
<bean class="com.prompt.cam.management.service.CheckLocaleInterceptor"/>
<mvc:interceptor>
<mvc:mapping path="/*"/> <!-- 인터셉터가 반응할 매핑 경로를 정의한다. -->
<bean class="com.prompt.cam.management.service.CheckLocaleInterceptor"/> <!-- 해당 클래스를 통해 인터셉터가 동작한다. -->
</mvc:interceptor>
</mvc:interceptors>
[CheckLocaleInterceptor.java]
package com.test.service;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Service
public class CheckLocaleInterceptor extends HandlerInterceptorAdapter { // HandlerInterceptorAdapter를 상속받은 클래스를 생성
@Override
public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler){ // 컨트롤러 접근전에 가로채기 위해 preHandle 사용
HttpSession session = request.getSession();
Locale locale = (Locale) session.getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
if (locale == null) {
// set default language 'ko'
session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.KOREAN);
return true;
} else {
// Not null skip
return true;
}
}
}
댓글 없음:
댓글 쓰기