Spring MVC使用SessionLocaleResolver实现用户自定义切换语言实例

在许多成熟的商业软件系统中可以让用户自由切换语言,而不是修改浏览器的语言设置。一旦用户选择了自己需要使用的语言环境,整个系统的语言环境将一直是这种语言环境。

Spring MVC 也可以允许用户自行选择程序语言。本章通过 Web 应用 springMVCDemo09 演示用户自定义切换语言,在该应用中使用 SessionLocaleResolver 实现国际化,具体步骤如下:

1)创建应用

创建应用 springMVCDemo09,并导入 Spring MVC 相关的 JAR 包。

2)创建国际化资源文件

在 WEB-INF/resource 目录下创建中英文资源文件 messages_en_US.properties 和 messages_zh_CN.properties。

messages_en_US.properties 的内容如下:

  1. first=first
  2. second=second
  3. third={0} third{1}
  4. language.en=English
  5. language.cn=Chinese

messages_zh_CN.properties 的内容如下:

  1. first=\u7B2C\u4E00\u9875
  2. second=\u7B2C\u4E8C\u9875
  3. third={0} \u7B2C\u4E09\u9875 {1}
  4. language.cn=\u4E2D\u6587
  5. language.en=\u82F1\u6587

3)创建视图 JSP 文件

在 WEB-INF/jsp 目录下创建 3 个 JSP 文件,即 first.jsp、second.jsp 和 third.jsp。

first.jsp 的代码如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <a href="${pageContext.request.contextPath }/i18nTest?locale=zh_ CN">
  12. <spring:message code="language.cn" /> </a> --
  13. <a href="${pageContext.request.contextPath }/i18nTest?locale=en_US">
  14. <spring:message code="language.en" /> </a>
  15. <br>
  16. <br>
  17. <spring:message code="first" />
  18. <br>
  19. <br>
  20. <a href="${pageContext.request.contextPath }/my/second">
  21. <spring:message code="second" /> </a>
  22. </body>
  23. </html>

second.jsp 的代码如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <spring:message code="second"/><br><br>
  12. <a href="${pageContext.request.contextPath }/my/third">
  13. <spring:message code="third" arguments="888,999"/>
  14. </a>
  15. </body>
  16. </html>

third.jsp 的代码如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <spring:message code="third" arguments="888,999" />
  12. <br>
  13. <br>
  14. <a href="${pageContext.request.contextPath }/my/first">
  15. <spring:message code="first" />
  16. </a>
  17. </body>
  18. </html>

4)创建控制器类

该应用有两个控制器类,一个是 I18NTestController 处理语言种类选择请求,一个是 MyController 进行页面导航。在 src 目录中创建一个名为 controller 的包,并在该包中创建这两个控制器类。

I18NTestController.java 的代码如下:

  1. package controller;
  2. import java.util.Locale;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. public class I18NTestController {
  6. @RequestMapping("/i18nTest")
  7. /**
  8. * locale接收请求参数locale值,并存储到session中
  9. */
  10. public String first(Locale locale) {
  11. return "first";
  12. }
  13. }

MyController 的代码如下:

  1. package controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. @RequestMapping("/my")
  6. public class MyController {
  7. @RequestMapping("/first")
  8. public String first() {
  9. return "first";
  10. }
  11. @RequestMapping("/second")
  12. public String second() {
  13. return "second";
  14. }
  15. @RequestMapping("/third")
  16. public String third() {
  17. return "third";
  18. }
  19. }

5)创建配置文件

在 WEB-INF 目录下创建配置文件 springmvc-servlet.xml 和 web.xml。web.xml 的代码与 Spring MVC 简单应用的相同,这里不再赘述。springmvc-servlet.xml 的代码如下:

  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:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/mvc
  13. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  14. <!-- 使用扫描机制扫描包 -->
  15. <context:component-scan base-package="controller" />
  16. <!-- 配置视图解析器 -->
  17. <bean
  18. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  19. <property name="prefix" value="/WEB-INF/jsp/" />
  20. <property name="suffix" value=".jsp" />
  21. </bean>
  22. <!-- 国际化操作拦截器,如果采用基于Session/Cookie则必须配置 -->
  23. <mvc:interceptors>
  24. <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
  25. </mvc:interceptors>
  26. <!-- 存储区域设置信息 -->
  27. <bean id="localeResolver"
  28. class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
  29. <property name="defaultLocale" value="zh_CN"></property>
  30. </bean>
  31. <!-- 加载国际化资源文件 -->
  32. <bean id="messageSource"
  33. class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  34. <!-- <property name="basename" value="classpath:messages" /> -->
  35. <property name="basename" value="/WEB-INF/resource/messages" />
  36. </bean>
  37. </beans>

6)发布应用并测试

首先将 springMVCDemo09 应用发布到 Tomcat 服务器并启动 Tomcat 服务器,然后通过地址“http://localhost:8080/springMVCDemo08/my/first”测试第一个页面,运行结果如图 1 所示。


图 1 中文环境下 first.jsp 的运行结果

单击图 1 中的“第二页”超链接,打开 second.jsp 页面,运行结果如图 2 所示。


图 2 中文环境下second.jsp的运行结果

 

单击图 2 中的“第三页”超链接,打开 third.jsp 页面,运行结果如图 3 所示。


图 3 中文环境下third.jsp的运行结果

单击图 1 中的“英文”超链接,打开英文环境下的 first.jsp 页面,运行结果如图 4 所示。


图 4 英文环境下 first.jsp 的运行结果

单击图 4 中的 second 超链接,打开英文环境下的 second.jsp 页面,运行结果如图 5 所示。


图 5 英文环境下 second.jsp 的运行结果

单击图 5 中的 third 超链接,打开英文环境下的 third.jsp 页面,运行结果如图 6 所示。


图 6 英文环境下 third.jsp 的运行结果

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智慧浩海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值