Spring MVC international

A, Spring MVC international Profile
       Program is a basic requirement of international commercial system, because today's software system is no longer a simple stand-alone program, are often an open system, need to face the visitors from all over the world, therefore, become an international business an essential part of the system.
       Spring MVC's internationalization is based on Java internationalization of the above, it is also the same in different countries by providing information resources / locale, and then specify the Locale corresponding resource files loaded by Resource Bundle, and then get the resource file specified key corresponding message. This whole process is identical with the international Java program, but Spring MVC framework Java program internationalization was further packaging, thus simplifying the international application.
 
Two, the Spring MVC internationalization of knowledge:
     1, messageSource Interface: Tell the storage location of the file system of international resources.         
               org.springframework.context.support.ResourceBundleMessageSource类
 
     2, LocaleResolver Interface: determining locales
          (1) the Accept-Langage: locale --- choose a browser-based default, no need to configure
          (2) SessionLocaleResolver: area selection session-based language, configure (common)
          (3) CookieLocaleResolver: area selection based on language Cookie, configure
    
     . 3, the LocaleChangeInterceptor blockers: international interceptors, when the locale is changed, the interceptor will intercept the locale is changed in accordance with the application parameters passed. SpringMVC need to be registered in the configuration file
      4, message Tags: is Spring MVC label, international news output in the view page
         <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
 
Three, the Spring MVC internationalization steps:
   (1) to the system load internationalization resource files.
   (2) the output of internationalization. Spring MVC international news output in two ways:
         A, international news output on the View page, you need to use Spring MVC tag library.
         B, the output of the message in international Controller processing method, it is necessary to use the getMessage org.springframework.web.servlet.support Requestcontext () method to complete.
 
Fourth, based on SessionLocaleResolver internationalization:
      1, create a resource file
         messages_zh_CN.properties
         messages_en_US.properties
 
      2, the configuration of the international class and interceptors
         <! - SessionLocaleResolver based locale selector ->
       <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
       <-! Location registration MessageSource, clear resource file ->
       <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
          <property name="basename" value="messages"></property>
       </bean>
 
       <! - Configure interceptor ->
       <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>
       
 
 
 
 
 
 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/lone5wolf/p/10943869.html