本文主要介绍如何使用springMVC实现国际化,项目使用jdk8+springMVC4.2.8+Velocity,全注解配置
Maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
   | <properties>     <jdk.version>1.8</jdk.version>     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>     <spring.version>4.2.8.RELEASE</spring.version> </properties>
  <dependencies>          <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring-webmvc</artifactId>         <version>${spring.version}</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring-context-support</artifactId>         <version>${spring.version}</version>     </dependency>
           <dependency>         <groupId>org.apache.velocity</groupId>         <artifactId>velocity</artifactId>         <version>1.7</version>     </dependency>     <dependency>         <groupId>org.apache.velocity</groupId>         <artifactId>velocity-tools</artifactId>         <version>2.0</version>     </dependency>
           <dependency>         <groupId>com.fasterxml.jackson.core</groupId>         <artifactId>jackson-databind</artifactId>         <version>2.8.5</version>     </dependency>
           <dependency>         <groupId>org.slf4j</groupId>         <artifactId>slf4j-log4j12</artifactId>         <version>1.7.25</version>     </dependency>
           <dependency>         <groupId>javax.servlet</groupId>         <artifactId>javax.servlet-api</artifactId>         <version>3.0.1</version>         <scope>provided</scope>     </dependency> </dependencies>
 
  <build>     <finalName>miscwebapp</finalName>     <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-compiler-plugin</artifactId>             <version>3.5.1</version>             <configuration>                 <source>${jdk.version}</source>                 <target>${jdk.version}</target>                 <showWarnings>true</showWarnings>                 <encoding>${project.build.sourceEncoding}</encoding>             </configuration>         </plugin>         <plugin>             <groupId>org.eclipse.jetty</groupId>             <artifactId>jetty-maven-plugin</artifactId>             <version>9.2.22.v20170606</version>             <configuration>                 <httpConnector>                     <port>8081</port>                 </httpConnector>                 <webApp>                     <contextPath>/</contextPath>                 </webApp>                 <scanIntervalSeconds>10</scanIntervalSeconds>                 <stopKey>foo</stopKey>                 <stopPort>9998</stopPort>             </configuration>         </plugin>     </plugins> </build>
   | 
 
注解全局配置类
ApplicationConfig.java
该类可以理解为spring.xml的配置类
1 2 3 4
   | @Configuration @ComponentScan(basePackages = "xyz.iyichen.misc") public class ApplicationConfig { }
   | 
 
WebMvcConfig.java
该类可以理解为spring-mvc.xml的配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   | @EnableWebMvc @Configuration @ComponentScan(basePackages = "xyz.iyichen.misc.web.controller") public class WebMvcConfig extends WebMvcConfigurerAdapter {
      @Bean     public VelocityConfigurer velocityConfig() {         Map<String, Object> config = new HashMap<>();         config.put("input.encoding", "UTF-8");         config.put("output.encoding", "UTF-8");
          VelocityConfigurer velocityConfigurer = new VelocityConfigurer();         velocityConfigurer.setResourceLoaderPath("/WEB-INF/velocity/");         velocityConfigurer.setVelocityPropertiesMap(config);         return velocityConfigurer;     }
      @Bean     public VelocityViewResolver viewResolver() {         VelocityViewResolver viewResolver = new VelocityViewResolver();         viewResolver.setCache(false);         viewResolver.setSuffix(".vm");         viewResolver.setRequestContextAttribute("requestContext");         viewResolver.setContentType("text/html;charset=UTF-8");         viewResolver.setDateToolAttribute("dateTool");         return viewResolver;     }
  }
   | 
 
ApplicationInitializer.java
该类可以理解为web.xml的配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
      @Override     protected Class<?>[] getRootConfigClasses() {                  return new Class[]{ApplicationConfig.class};     }
      @Override     protected Class<?>[] getServletConfigClasses() {                  return new Class[]{WebMvcConfig.class};     }
      @Override     protected String[] getServletMappings() {                  return new String[]{"*.htm"};     } }
  | 
 
国际化配置
创建国际化配置文件
新建文件夹i18n,并在文件夹下新建国际化资源文件,结构如下:
1 2 3 4 5 6 7 8 9
   | - src/main/     - java     - resources         - i18n             - server_en.properties // 英语配置             - server_zh_TW.properties // 繁体中文配置             - server_zh.properties // 简体中文配置             - server.properties // 默认配置     - webapp
   | 
 
在资源文件中写入如下内容:
1 2 3 4 5 6 7 8 9 10 11
   | # 在server_en.properties写入 say.hello=hello
  # 在server_zh_TW.properties写入 say.hello=妳好
  # 在server_zh.properties写入 say.hello=你好
  # 在server.properties写入 say.hello=helloa
   | 
 
添加国际化支持
修改WebMvcConfig.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
   | @EnableWebMvc @Configuration @ComponentScan(basePackages = "xyz.iyichen.misc.web.controller") public class WebMvcConfig extends WebMvcConfigurerAdapter {
      @Override     public void addInterceptors(InterceptorRegistry registry) {         registry.addInterceptor(localeChangeInterceptor());     }
      @Bean     public SessionLocaleResolver localeResolver() {         SessionLocaleResolver localeResolver = new SessionLocaleResolver();         localeResolver.setDefaultLocale(Locale.ENGLISH);         return localeResolver;     }
      @Bean     public LocaleChangeInterceptor localeChangeInterceptor() {         LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();         localeChangeInterceptor.setParamName("lang");         localeChangeInterceptor.setIgnoreInvalidLocale(true);         return localeChangeInterceptor;     }
      @Bean     public ReloadableResourceBundleMessageSource messageSource() {         ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();         messageSource.setBasenames("classpath:i18n/server");         messageSource.setDefaultEncoding("UTF-8");         messageSource.setFallbackToSystemLocale(false);         return messageSource;     }
           @Bean     public VelocityConfigurer velocityConfig() {         Map<String, Object> config = new HashMap<>();         config.put("input.encoding", "UTF-8");         config.put("output.encoding", "UTF-8");
          VelocityConfigurer velocityConfigurer = new VelocityConfigurer();         velocityConfigurer.setResourceLoaderPath("/WEB-INF/velocity/");         velocityConfigurer.setVelocityPropertiesMap(config);         return velocityConfigurer;     }
      @Bean     public VelocityViewResolver viewResolver() {         VelocityViewResolver viewResolver = new VelocityViewResolver();         viewResolver.setCache(false);         viewResolver.setSuffix(".vm");         viewResolver.setRequestContextAttribute("requestContext");         viewResolver.setContentType("text/html;charset=UTF-8");         viewResolver.setDateToolAttribute("dateTool");         return viewResolver;     } }
   | 
 
页面测试
新建测试页面test.vm
结构如下:
1 2 3 4 5 6 7
   | - src/main/     - java     - resources     - webapp         - WEB-INF             - velocity                 - test.vm
   | 
 
写入以下内容:
1
   | #springMessage("say.hello") 
  | 
 
新建测试类TestController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | package xyz.iyichen.misc.web.controller;
  import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
  @Controller @RequestMapping public class TestController {
      @RequestMapping("/test")     public String test() {         return "test";     } }
   | 
 
访问测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | # 默认语言为英语,使用server_en.properties # http://localhost:8081/test.htm  页面输出-> hello 
  # 需求语言为英语,使用server_en.properties # http://localhost:8081/test.htm?lang=en  页面输出-> hello
  # 需求语言为简体中文,使用server_zh.properties # http://localhost:8081/test.htm?lang=zh  页面输出-> 你好
  # 需求语言为繁体中文,使用server_zh_TW.properties # http://localhost:8081/test.htm?lang=zh_TW  页面输出-> 妳好 
  # 需求语言为法语,服务端不支持该语言,使用server.properties # http://localhost:8081/test.htm?lang=fr 页面输出-> helloa
   |