1. servlet-context.xml
<!-- 언어 정보를 세션에 저장하여 사용합니다. -->
<beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<beans:property name="defaultLocale" value="ko"/>
</beans:bean>
<interceptors>
<beans:bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
</interceptors>
2. Controller 설정
private org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MainController.class);
@Autowired
SessionLocaleResolver localeResolver;
@Autowired
MessageSource messageSource;
@RequestMapping(value = "/i18n.do", method = RequestMethod.GET)
public String i18n(Locale locale, HttpServletRequest request, Model model) {
// RequestMapingHandler로 부터 받은 Locale 객체를 출력해 봅니다.
logger.info("Welcome i18n! The client locale is {}.", locale);
// localeResolver 로부터 Locale 을 출력해 봅니다.
logger.info("Session locale is {}.", localeResolver.resolveLocale(request),
localeResolver.resolveLocale(request));
//
logger.info("LABEL.APPNAME", messageSource.getMessage("LABEL.APPNAME", null, locale));
// JSP 페이지에서 EL 을 사용해서 arguments 를 넣을 수 있도록 값을 보낸다.
model.addAttribute("siteCount", messageSource.getMessage("LABEL.APPNAME", null, locale));
return "common/main";
}
3. jsp 설정
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
Hello World!!
<spring:eval expression="@configProperties['mysql.username']"> </spring:eval>
<p>LABEL.APPNAME : <spring:message code="LABEL.APPNAME" text="default text" /></p>
<p> <a href="<c:url value="/i18n.do?lang=ko" />">한국어</a> <a href="<c:url value="/i18n.do?lang=en" />">English</a>
<p>site.count using EL : <spring:message code="LABEL.APPNAME" arguments="${siteCount}" text="default text" /></p>
${siteCount}
</body>
</html>
'개발 > 개발환경설정' 카테고리의 다른 글
Spring message ResourceBundle 설정 (0) | 2020.08.11 |
---|---|
Spring property 암호화 (jasypt) (0) | 2020.08.11 |
util:properties 설정 (0) | 2020.08.10 |
Tiles 설정 (0) | 2020.08.10 |
Filter 설정 (0) | 2020.03.18 |