21.Spring MVC国际化

国际化(Internationalization 简称 I18n,其中“I”和“n”分别为首末字符,18 则为中间的字符数)是指软件开发时应该具备支持多种语言和地区的功能。

换句话说,软件应该能够同时应对多个不同国家和地区用户的访问,并根据用户地区和语言习惯,提供相应的、符合用具阅读习惯的页面和数据。例如,为中国用户提供汉语界面显示,为美国用户提供提供英语界面显示。

本节我们以 Thymemeaf 为例,演示下如何在 Spring MVC 项目中实现国际化。

在 Spring 项目中实现国际化,通常需要以下 4 步:

  1. Spring MVC 配置文件进行配置;
  2. 编写国际化资源(配置)文件;
  3. 在页面中获取国际化内容;
  4. 编写控制器方法手动切换语言。

1. 配置 Spring MVC 的配置文件

想要在 Spring MVC 中实现国际化,我们需要在 Spring MVC 的配置文件中配置以下内容:

  1. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  2. <property name="basenames" value="messages"></property>
  3. <property name="defaultEncoding" value="UTF-8"></property>
  4. <property name="cacheSeconds" value="0"></property>
  5. </bean>
  6.  
  7. <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
  8. <property name="defaultLocale" value="en_US"/>
  9. </bean>
  10.  
  11. <mvc:interceptors>
  12. <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
  13. <property name="paramName" value="lang"></property>
  14. </bean>
  15. </mvc:interceptors>


以上个配置说明如下:

  • ResourceBundleMessageSource:对资源文件中设置的内容进行绑定;
  • LocaleChangeInterceptor:用于获取请求中的国际化信息并将其转换为 Locale 对象,以获取 LocaleResolver 对象对国际化资源文件进行解析。
  • SessionLocaleResolver:将将包含了国际化信息的 Locale 对象存储在 Session 中,供后续页面继续使用。

2. 编写国际化资源文件

在 Spring MVC 工程的类路径下创建国际化资源文件,文件名格式为:基本名_语言代码_国家或地区代码,例如 messages_en_US.properties、messages_zh_CN.properties。

在国际化资源文件中,我们需要对不同语言环境下需要进行国际化的各个字段进行配置,示例代码如下。

messages_en_US.properties 代码如下:

userName=userName
password=password


messages_zh_CN.properties 代码如下:

userName=用户名
password=密码


需要注意的是,这国际化资源文件名称必须严格按照其命名格式进行命名,否则解析时会出错。其次,国际化资源文件在创建完成后,这些文件通常会自动归档(Resouce Bundle),而该目录并非我们手动创建,如下所示。

- resources
    - Resouce Bundle 'messages'
        - messages_en_US.properties
        - messages_zh_CN.properties

3. 在页面中获取国际化内容

在完成以上配置后,我们只要在 Thymeleaf 代码中获取指定的字段即可,示例代码如下。

  1. <h2 th:text="#{userName}"></h2>
  2. <h2 th:text="#{password}"></h2>

4. 手动切换语言环境

我们可以在控制器类中编写一个控制器方法,手动切国际化的语言环境。

1) 在前端页面中添加切换语言环境的超链接,示例代码如下。

  1. <a th:href="@{/localeChange(lang=en_US)}">英文</a>
  2. <a th:href="@{/localeChange(lang=zh_CN)}">中文</a>


2) 在控制器类中添加一个切换或计划语言环境的控制器方法,示例代码如下。

  1. @Controller
  2. public class TestController {
  3.     @Resource
  4.     private ResourceBundleMessageSource messageSource;
  5.  
  6.     //切换语言环境
  7.     @RequestMapping("/localeChange")
  8.     public String localeChange(Locale locale) {
  9.         String userName = messageSource.getMessage("userName", null, locale);
  10.         String password = messageSource.getMessage("password", null, locale);
  11.         System.out.println(userName + "----" + password);
  12.         return "user";
  13.     }
  14. }


当我们想要手动切换语言环境时,只需要点击相应的超链接即可,此时浏览会发送一个 “/localeChange?lang=***”的请求。该请求会被我们在 Spring MVC 配置文件中配置的 LocaleChangeInterceptor 拦截,并将指定的请求参数(lang)的值转换为 Locale 对象,以获取 LocaleResolver 对象对指定的国际化资源文件进行解析,最终实现切换国际化语言环境的目的。

示例

下面,我们通过一个简单的实例,完整地演示下 Spring MVC+Thymeleaf 实现国际化的过程。

1. 新建一个名为 springmvc-I18n-demo 的 Web 工程,并将 Spring MVC 相关的依赖引入到工程中,web.xml 配置内容如下。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6.  
  7. <!--请求和响应的字符串过滤器-->
  8. <filter>
  9. <filter-name>CharacterEncodingFilter</filter-name>
  10. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  11. <init-param>
  12. <param-name>encoding</param-name>
  13. <param-value>UTF-8</param-value>
  14. </init-param>
  15. <init-param>
  16. <param-name>forceResponseEncoding</param-name>
  17. <param-value>true</param-value>
  18. </init-param>
  19. </filter>
  20. <filter-mapping>
  21. <filter-name>CharacterEncodingFilter</filter-name>
  22. <url-pattern>/*</url-pattern>
  23. </filter-mapping>
  24.  
  25. <!--来处理 PUT 和 DELETE 请求的过滤器-->
  26. <filter>
  27. <filter-name>HiddenHttpMethodFilter</filter-name>
  28. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  29. </filter>
  30. <filter-mapping>
  31. <filter-name>HiddenHttpMethodFilter</filter-name>
  32. <url-pattern>/*</url-pattern>
  33. </filter-mapping>
  34.  
  35. <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 -->
  36. <servlet>
  37. <servlet-name>dispatcherServlet</servlet-name>
  38. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  39. <init-param>
  40. <param-name>contextConfigLocation</param-name>
  41. <param-value>classpath:springMVC.xml</param-value>
  42. </init-param>
  43. <load-on-startup>1</load-on-startup>
  44. </servlet>
  45.  
  46. <servlet-mapping>
  47. <servlet-name>dispatcherServlet</servlet-name>
  48. <url-pattern>/</url-pattern>
  49. </servlet-mapping>
  50. </web-app>


2. 在 src 目录(类路径)下新建一个名为 springMVC.xml 的 Spring MVC 配置文件,配置内容如下。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  10.  
  11. <!--开启组件扫描-->
  12. <context:component-scan base-package="net.biancheng.c"></context:component-scan>
  13.  
  14. <!-- 配置 Thymeleaf 视图解析器 -->
  15. <bean id="viewResolver"
  16. class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
  17. <property name="order" value="1"/>
  18. <property name="characterEncoding" value="UTF-8"/>
  19. <property name="templateEngine">
  20. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
  21. <property name="templateResolver">
  22. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
  23. <!-- 视图前缀 -->
  24. <property name="prefix" value="/WEB-INF/templates/"/>
  25. <!-- 视图后缀 -->
  26. <property name="suffix" value=".html"/>
  27. <property name="templateMode" value="HTML5"/>
  28. <property name="characterEncoding" value="UTF-8"/>
  29. </bean>
  30. </property>
  31. </bean>
  32. </property>
  33. </bean>
  34.  
  35. <mvc:annotation-driven/>
  36.  
  37. <mvc:view-controller path="/" view-name="login"></mvc:view-controller>
  38.  
  39. <mvc:view-controller path="/success" view-name="success"></mvc:view-controller>
  40.  
  41. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  42. <property name="basenames" value="messages"></property>
  43. <property name="defaultEncoding" value="UTF-8"></property>
  44. <property name="cacheSeconds" value="0"></property>
  45. </bean>
  46.  
  47. <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
  48. <property name="defaultLocale" value="en_US"/>
  49. </bean>
  50.  
  51. <mvc:interceptors>
  52. <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
  53. <property name="paramName" value="lang"></property>
  54. </bean>
  55. </mvc:interceptors>
  56. </beans>


3. 在 net.biancheng.c.entity 包下,创建一个名为 User 的实体类,代码如下。

  1. package net.biancheng.c.entity;
  2.  
  3. public class User {
  4. private String userName;
  5. private String password;
  6.  
  7. public String getUserName() {
  8. return userName;
  9. }
  10.  
  11. public void setUserName(String userName) {
  12. this.userName = userName;
  13. }
  14.  
  15. public String getPassword() {
  16. return password;
  17. }
  18.  
  19. public void setPassword(String password) {
  20. this.password = password;
  21. }
  22.  
  23. @Override
  24. public String toString() {
  25. return "User{" +
  26. "userName='" + userName + '\'' +
  27. ", password='" + password + '\'' +
  28. '}';
  29. }
  30. }


4. 在 net.biancheng.c.controller 包下,创建一个名为 I18nController 的控制器类,代码如下。

  1. package net.biancheng.c.controller;
  2.  
  3. import org.springframework.context.support.ResourceBundleMessageSource;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6.  
  7. import javax.annotation.Resource;
  8. import java.util.Locale;
  9.  
  10. @Controller
  11. public class I18nController {
  12. @Resource
  13. private ResourceBundleMessageSource messageSource;
  14.  
  15. //切换语言环境
  16. @RequestMapping("/localeChange")
  17. public String localeChange(Locale locale) {
  18. String userName = messageSource.getMessage("userName", null, locale);
  19. String password = messageSource.getMessage("password", null, locale);
  20. String submit = messageSource.getMessage("submit", null, locale);
  21. String reset = messageSource.getMessage("reset", null, locale);
  22. String error = messageSource.getMessage("error", null, locale);
  23. System.out.println(userName + "----" + password + "----" + submit + "----" + reset + "----" + error);
  24. return "/login";
  25. }
  26. }


5. 在 net.biancheng.c.controller 包下,创建一个名为 LoginController 的控制器类,代码如下。

  1. package net.biancheng.c.controller;
  2.  
  3. import net.biancheng.c.entity.User;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7.  
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpSession;
  10.  
  11. @Controller
  12. public class LoginController {
  13. @RequestMapping(value = "/login", method = RequestMethod.POST)
  14. public String login(User user, HttpServletRequest request) {
  15. if ("admin".equals(user.getUserName()) && "admin".equals(user.getPassword())) {
  16. HttpSession session = request.getSession();
  17. session.setAttribute("loginUser", user);
  18. return "redirect:/success";
  19. }
  20. request.setAttribute("msg", "error");
  21. return "login";
  22. }
  23. }


6. 在类目录下,新建两个国际化资源文件:messages_en_US.properties 和 messages_zh_CN.properties,内容如下。

messages_en_US.properties 配置内容如下:

userName=userName
password=password
submit=submit
reset=reset
error=wrong user name or password!
welcome=Welcome

messages_zh_CN.properties 配置内容如下:

userName=用户名
password=密码
submit=提交
reset=重置
error=用户名或密码错误!
welcome=欢迎您


7. 在 webapp/WEB-INF 下新建一个名为 templates 目录,并在该目录下创建一个名为 login.html 的登录页,代码如下。

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <form th:action="@{/login}" method="post">
  9. <table>
  10. <tr>
  11. <td colspan="2" align="center">
  12. <p style="color: red;margin: auto" th:text="#{error}" th:if="${not #strings.isEmpty(msg)}"></p>
  13. </td>
  14. </tr>
  15. <tr>
  16. <td th:text="#{userName}"></td>
  17. <td><input type="text" name="userName" required><br></td>
  18. </tr>
  19. <tr>
  20. <td th:text="#{password}"></td>
  21. <td><input type="password" name="password" required><br></td>
  22. </tr>
  23.  
  24. <tr>
  25. <td colspan="2" align="center">
  26. <input type="submit" th:value="#{submit}">
  27. <input type="reset" th:value="#{reset}">
  28. </td>
  29. </tr>
  30. </table>
  31. </form>
  32. <a th:href="@{/localeChange(lang=en_US)}">英文</a>
  33. <a th:href="@{/localeChange(lang=zh_CN)}">中文</a>
  34. </body>
  35. </html>


8. 在 webapp/WEB-INF/templates 下,创建一个 success.html,代码如下。

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>C语言中文网</title>
  6. </head>
  7. <body>
  8. <h1 th:text="#{welcome}+':'+${session.loginUser.getUserName()}"></h1>
  9. </table>
  10. </body>
  11. </html>


9. 将 springmvc-I18n-demo 部署到 Tomcat 服务器中,启动 Tomcat,使用浏览器访问“http://localhost:8080/springmvc-I18n-demo/”,结果如下图。


图1:登录页-英文


10. 分别输入错误的 userName 和 password ,如下图。


图2:账号密码-英文


11. 点击下方的“submit”按钮,结果如下图。 


图3:错误提示-英文


12. 在登录页输入正确的用户名(admin)和密码(admin),点击下方的“submit”按钮跳转到“登陆成功页”,如下图。


图4:登陆成功页-英文


13. 返回登录页,点击下方的“中文”将语言环境切换为中文,结果如下图。


图5:登录页-中文


14. 再次在登录页输入错误的用户名和密码,结果如下图。


图6:错误提示-中文


13. 在登录页分别输入用户名(admin)和密码(admin),点击下方的“提交”按钮,结果如下图。


图6:登陆成功-中文
posted @ 2022-07-31 14:44  随遇而安==  阅读(144)  评论(0编辑  收藏  举报