• 基于SpringMVC国际化资源配置方式


    1.首先需要在spring-mvc-servlet.xml 中配置拦截器;

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />

    2.如果要浏览器自适应语言方式(cookie方式):还需要在spring-mvn-servlet.xml 中配置:

    2.1

    <bean id="cookieLocaleResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>

    2.2.如果是需要在界面上进行切换(session方式);则需要配置:

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
            <property name="defaultLocale" value="en_US"/>
        </bean>

    3.最后在applicationContext.xml 中配置关于资源文件

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="message"/>
            <property name="useCodeAsDefaultMessage" value="true"/>
        </bean>

    4.在拦截器中配置local ,代码如下:

    <mvc:interceptors>
    
            <!--国际化资源切换(根据请求参数中的locale参数自动切换)-->
            <mvc:interceptor>
                <mvc:mapping path="/**"/>
                <ref bean="localeChangeInterceptor"/>
            </mvc:interceptor>
        </mvc:interceptors>

    5.具体的message_zh_CN.properties, message_en_US.properties ,等文件,放置于classpath 下。

    6.最后,如果要切换,只需要在路径的 参数中,添加参数 locale=?即可。如下示例:

      http://www.woca.com/?locale=zh_CN 

      http://www.woca.com/?locale=en_US 

  • 相关阅读:
    C语言的AES加密
    curl指定域名的IP
    gdb调试知识
    C++获取寄存器eip的值
    C++嵌入lua
    [置顶] python字典和nametuple互相转换例子
    【python】redis基本命令和基本用法详解
    xshell登录到CentOS7上时出现“The remote SSH server rejected X11 forwarding request.
    selinue引起的ssh连接错误
    SCP和SFTP相同点和区别
  • 原文地址:https://www.cnblogs.com/zhangtan/p/7609951.html
Copyright © 2020-2023  润新知