zoukankan      html  css  js  c++  java
  • 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"/>">
                 &nbsp;&nbsp;&nbsp;&nbsp;
                 <input type="reset" value="<spring:message code="reset"/>">
              </div>
           
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    查询对象模式(下)
    淘宝code
    C#中使用消息队列RabbitMQ
    MVC5模板部署到mono
    ventBroker简单实现
    http协议知识整理(转)
    创业者应该有的5个正常心态(转)
    观点:独立游戏开发者创业路上的11个‘坑’(转)
    应用程序框架实战三十四:数据传输对象(DTO)介绍及各类型实体比较(转)
    【技巧篇】解决悬浮的<header>、<footer>遮挡内容的处理技巧(转)
  • 原文地址:https://www.cnblogs.com/lone5wolf/p/10943869.html
Copyright © 2011-2022 走看看