本文使用 Spring Web model-view-controller (MVC) framework
建立一個簡單的一個Hello World 程式
1、請參考建立一個SpringMVC Annotation Project - Hello World此篇文章,以下是以此作為延伸
2. 文件放置位置
3. 建立Controller & Mapping
在Spring 3 裡大量使用 annotation 。
如:
@Controller
目錄 : src/main/java/com/sample/web/controller
檔名 : HelloController.java
程式碼:
package com.sample.web.controller; import java.util.Locale; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController{ @Autowired MessageSource messageSource; @RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET) public ModelAndView welcomePage() { System.out.println(messageSource.getMessage( "hello.springmvc", null, Locale.CHINA)); System.out.println(messageSource.getMessage( "hello.springmvc", null, Locale.ENGLISH)); System.out.println(messageSource.getMessage( "hello.springmvc", null, Locale.TAIWAN)); return new ModelAndView("hello"); } }
5. 建立JSP 頁面
目錄 : src/main/webapp/WEB-INF/pages/
檔名 : hello.jsp
程式碼:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <html> <body> Language : <a href="?language=en">English</a>| <a href="?language=zh_CN">Chinese</a>| <a href="?language=zh_TW">Taiwan</a> <h3> hello.springmvc : <spring:message code="hello.springmvc" text="default text" /> </h3> Current Locale : ${pageContext.response.locale} </body> </html>
6. 建立properties 頁面
目錄 : src/main/resources/i18n
檔名 : messages_en.properties
檔名 : messages_zh_CN.properties
檔名 : messages_zh_TW.properties7. 建立AppConfig.java (Spring設定)
目錄 : src/main/java/com/sample/config
檔名 : AppConfig.java
package com.sample.config; import java.util.Locale; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @EnableWebMvc @Configuration @ComponentScan({ "com.sample.web.*" }) public class AppConfig extends WebMvcConfigurerAdapter{ /** * 加入SPRING攔截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } /** * i18n 國際化(可改變區域) * @return HandlerInterceptor */ @Bean public HandlerInterceptor localeChangeInterceptor() { LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); localeChangeInterceptor.setParamName("language"); return localeChangeInterceptor; } /** * 區域解析器 * @return LocaleResolver */ @Bean public LocaleResolver localeResolver() { SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver(); sessionLocaleResolver.setDefaultLocale(Locale.TAIWAN); return sessionLocaleResolver; } /** * i18n文件放置位置 * @return MessageSource */ @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setUseCodeAsDefaultMessage(true); messageSource.setBasename("classpath:i18n/messages"); messageSource.setDefaultEncoding("UTF-8"); messageSource.setCacheSeconds(0); return messageSource; } /** * 頁面導向 * @return */ @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/pages/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
7. 結果呈現
A.預設語言:
B.簡體中文:
C.繁體中文:
D.英文:
下一篇:
[JAVA_Spring]建立一個SpringMVC Annotation Project 3 -2 國際化(i18n) - LocaleResolver 詳解
其它文章
沒有留言:
張貼留言