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 的内容如下:
first=first
second=second
third={0} third{1}
language.en=English
language.cn=Chinese
messages_zh_CN.properties 的内容如下:
first=/u7B2C/u4E00/u9875
second=/u7B2C/u4E8C/u9875
third={0} /u7B2C/u4E09/u9875 {1}
language.cn=/u4E2D/u6587
language.en=/u82F1/u6587
在 WEB-INF/jsp 目录下创建 3 个 JSP 文件,即 first.jsp、second.jsp 和 third.jsp。
first.jsp 的代码如下:
%@ page language= java contentType= text/html; charset=UTF-8 pageEncoding= UTF-8 % %@taglib prefix= spring uri= http://www.springframework.org/tags % !DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd html head meta http-equiv= Content-Type content= text/html; charset=UTF-8 title Insert title here /title /head body a href= ${pageContext.request.contextPath }/i18nTest?locale=zh_ CN spring:message code= language.cn / /a -- a href= ${pageContext.request.contextPath }/i18nTest?locale=en_US spring:message code= language.en / /a spring:message code= first / a href= ${pageContext.request.contextPath }/my/second spring:message code= second / /a /body /html
second.jsp 的代码如下:
%@ page language= java contentType= text/html; charset=UTF-8 pageEncoding= UTF-8 % %@taglib prefix= spring uri= http://www.springframework.org/tags % !DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd html head meta http-equiv= Content-Type content= text/html; charset=UTF-8 title Insert title here /title /head body spring:message code= second / br br a href= ${pageContext.request.contextPath }/my/third spring:message code= third arguments= 888,999 / /body /html
third.jsp 的代码如下:
%@ page language= java contentType= text/html; charset=UTF-8 pageEncoding= UTF-8 % %@taglib prefix= spring uri= http://www.springframework.org/tags % !DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd html head meta http-equiv= Content-Type content= text/html; charset=UTF-8 title Insert title here /title /head body spring:message code= third arguments= 888,999 / a href= ${pageContext.request.contextPath }/my/first spring:message code= first / /body /html4)创建控制器类
该应用有两个控制器类,一个是 I18NTestController 处理语言种类选择请求,一个是 MyController 进行页面导航。在 src 目录中创建一个名为 controller 的包,并在该包中创建这两个控制器类。
I18NTestController.java 的代码如下:
package controller; import java.util.Locale; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class I18NTestController { @RequestMapping( /i18nTest ) * locale接收请求参数locale值,并存储到session中 public String first(Locale locale) { return first }
MyController 的代码如下:
package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping( /my ) public class MyController { @RequestMapping( /first ) public String first() { return first @RequestMapping( /second ) public String second() { return second @RequestMapping( /third ) public String third() { return third }5)创建配置文件
在 WEB-INF 目录下创建配置文件 springmvc-servlet.xml 和 web.xml。web.xml 的代码与 Spring MVC 简单应用的相同,这里不再赘述。springmvc-servlet.xml 的代码如下:
?xml version= 1.0 encoding= UTF-8 ? beans xmlns= http://www.springframework.org/schema/beans xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance xmlns:mvc= http://www.springframework.org/schema/mvc xmlns:p= http://www.springframework.org/schema/p xmlns:context= http://www.springframework.org/schema/context xsi:schemaLocation= http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd !-- 使用扫描机制扫描包 -- context:component-scan base-package= controller / !-- 配置视图解析器 -- bean 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 的运行结果22588.html
htmljavaJSPServletspringSpringMVCWeb
相关文章
- Spring学习笔记(三)——Spring注解开发&spring与Junit整合测试
- spring boot自动配置原理面试题_Spring boot面试
- Spring+MyBatis实例详解「建议收藏」
- 【08】Spring源码-分析篇-Bean的实例化
- spring boot整合shiro_Spring框架介绍及使用
- 兵马未动,粮草先行——spring码前准备
- spring cloud 配置中心配置哪些东西_druid连接池配置优化
- 优雅!Spring的重试小工具
- 关于Spring Cloud Gateway与下游服务器的连接分析
- Spring Boot 3.0 要来了,真心强!
- Java 近期新闻:MicroProfile 6.0、Kotlin 1.8、Spring Framework 升级
- Spring Cloud 2020.0.4 发布!
- Spring 那么多子项目,谁才是真正的一哥?
- Spring Boot的日志管理
- 第一个Spring程序
- spring的AOP(五)—-Spring AOP 编程(AspectJ )详解编程语言
- spring 事务传播行为实例分析详解编程语言
- Spring+Mybatis+Maven+MySql搭建实例详解编程语言
- spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例详解编程语言
- Spring Boot(五):spring data jpa的使用详解编程语言
- 手写简易版Spring框架-1详解编程语言
- Spring CGLlB动态代理(附带实例)
- Spring MVC数据绑定和表单标签的应用(附带实例)