当前位置:
首页 >
前端技术
> javascript
>内容正文
javascript
Spring MVC国际化
生活随笔
收集整理的这篇文章主要介绍了
Spring MVC国际化
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
一、Spring MVC国际化简介 程序国际化是商业系统的一个基本要求,因为今天的软件系统不再是简单的单机程序,往往都是一个开放的系统,需要面对来自全世界各个地方的访问者,因此,国际化成为商业系统必不可少的一部分。 Spring MVC的国际化是建立在Java国际化的基础之上的,其一样也是通过提供不同国家/语言环境的消息资源,然后通过 Resource Bundle加载指定 Locale对应的资源文件,再取得该资源文件中指定key对应的消息。这整个过程与Java程序的国际化完全相同,只是 Spring MVC框架对Java程序国际化进行了进一步的封装,从而简化了应用程序的国际化。 二、Spring MVC国际化的知识: 1、messageSource接口:告诉系统国际资源文件的存储位置。 org.springframework.context.support.ResourceBundleMessageSource类 2、LocaleResolver接口:确定语言区域 (1)accept-langage:基于浏览器的语言区域选择 --- 默认方式,不需要配置 (2)SessionLocaleResolver:基于会话的语言区域选择,需要配置(常用) (3)CookieLocaleResolver:基于Cookie的语言区域选择,需要配置 3、LocaleChangeInterceptor拦截器:国际化的拦截器,当语言区域发生改变时,该拦截器将进行拦截,根据传递的参数来改变应用的语言环境。需要在SpringMVC的配置文件中进行注册 4、message标签:是Spring MVC的标签,在视图页面中输出国际化的消息 <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> 三、Spring MVC国际化步骤: (1)给系统加载国际化资源文件。 (2)输出国际化。 Spring MVC输出国际化消息有两种方式: A、在视图页面上输出国际化消息,需要使用 Spring MVC的标签库。 B、在 Controller的处理方法中输出国际化消息,需要使用 org.springframework.web.servlet.support Requestcontext的getMessage()方法来完成。 四、基于SessionLocaleResolver的国际化: 1、创建资源文件 messages_zh_CN.properties messages_en_US.properties 2、配置国际化的类和拦截器 <!-- 基于SessionLocaleResolver的语言区域选择器 --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> <!-- 注册MessageSource,明确资源文件的位置 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages"></property> </bean> <!-- 配置拦截器 --> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang"></property> </bean> </mvc:interceptors> 3、在页面中使用message标签输出国际化信息 <spring:message code="language"/>: <a href="?lang=zh_CN"> <spring:message code="language.cn"/> </a> ------ <a href="?lang=en_US"> <spring:message code="language.en"/> </a> <br><br> <div align="center"> <h2> <spring:message code="userlogin"/> </h2> <hr><br> <spring:message code="username"/>: <input type="text"> <br><br> <spring:message code="password"/> <input type="password"> <br><br> <input type="submit" value="<spring:message code="submit"/>"> <input type="reset" value="<spring:message code="reset"/>"> </div>
转载于:https://www.cnblogs.com/lone5wolf/p/10943869.html
《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读总结
以上是生活随笔为你收集整理的Spring MVC国际化的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: day04_列表
- 下一篇: SpringBoot概述