• SpringMVC 国际化


    SpringMVC 国际化

    作为当前比较流行的MVC框架,SpringMVC也支持国际化。国际化说白了就是可以设置不同的语言。

    Spring 国际化的步骤

    • 给系统定义国际化的资源文件。
    • 输出国际化,这需要两点的支持,
    • 1,在视图输出界面使用SpringMVC的标签库 spring:message
    • 2,在Controller的处理方法中,进行相应的处理。

    配置及编码

    springmvc-config.xml 配置信息

    messageSource

    首先要告诉springmvc多语言文件保存在哪里,这里使用的是messageSource bean。

    message_en_US.properties

    loginname= Login name:
    password = Password:
    submit = Submit
    

    message_zh_CN.properties

    loginname= 用户名:
    password = 密码:
    submit = 提交
    

    还有一个message.properties和message_en_US.properties内容相同

    <bean id="messageSource"  
        class="org.springframework.context.support.ResourceBundleMessageSource">  
        <!-- 国际化资源文件名 -->
        <property name="basenames" >
                <list>
                    <value>message</value>
                    <value>chris</value>
                </list>
            </property>
    </bean>
    

    其中basenames 用来指定国际化文件的名称,如果只有一个,也可以写成basename

    <bean id="messageSource"  
        class="org.springframework.context.support.ResourceBundleMessageSource">  
        <!-- 国际化资源文件名 -->
        <property name="basename" value="message"/>
    </bean>
    

    localeResolver

    在定义完资源信息后,就需要定义解析资源的类了,SpringMVC提供了一个语言解析器接口LocaleResolver 并提供了3个默认的实现:

    • AcceptHeaderLocaleResolver
    • SessionLocaleResolver
    • CookieLocaleResolver

    AcceptHeaderLocaleResovler是SpringMVC默认使用的配置,只要在springmvc-config.xml中使用默认配置即可。

    <mvc:annotation-driven/>
    

    如果想使用SessionLocleResolver或CookieLocaleResolver则要在配置文件中配置,配置如下,

    <mvc:interceptors>  
    <!-- 国际化操作拦截器 如果采用基于(Session/Cookie)则必需配置 --> 
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />  
    </mvc:interceptors>  
    
    <!-- SessionLocaleResolver 配置 -->
        <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> 
    

    上面的配置以SessionLocaleResolver为例,CookieLocaleResolver 配置类似,不在赘述。

    至此,关于xml配置的内容一介绍完毕。

    jsp页面中使用标签

    显示界面上使用spring的标签

    <%@taglib prefix= "form" uri= "http://www.springframework.org/tags/form" %>
    <%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>
    

    页面逻辑的jsp代码如下。

    <form:form modelAttribute="user" method="post" action="login" >
    	<table>
    		<tr>
    			<td><spring:message code="loginname"/></td>
    			<td><form:input path="loginname"/></td>
    		</tr>
    		<tr>
    			<td><spring:message code="password"/></td>
    			<td><form:input path="password"/></td>
    		</tr>
    		<tr>
    			<td><input type="submit" value="<spring:message code="submit"/>"/></td>
    		</tr>
    	</table>
    </form:form>
    

    其中spring:message 用于国际化显示,code指明绑定的字段。

    Controller中的特殊处理

    当请求发送到Controller后,后台代码要根据请求获得国际化信息

    • 如果使用的是AcceptheaderLocaleResolver,则使用下面的方法获得国际化信息。
    RequestContext requestContext = new RequestContext(request);
    String username = requestContext.getMessage("username");
    user.setUsername(username);
    model.addAttribute("user", user);
    return "success";
    
    • 如果使用的SessionLocaleResolver,则需要在在session中添加属性,进行语言环境切换。
    Locale locale = new Locale("zh", "CN"); 
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
    
    • 如果使用CoolieResolver,则也需要在Cookie中设置语言环境
    Locale locale = new Locale("zh", "CN");
        (new CookieLocaleResolver()).setLocale (request, response, locale);
    
    I am chris, and what about you?
  • 相关阅读:
    SpringBoot配置文件数据格式三种写法
    实施工程师
    Linux命令大全
    PDCA原则
    cmd命令下载maven管理需要的依赖jar包
    java集合体系结构总结
    回溯
    红黑树详解
    Java 垃圾回收算法
    MySQL优化:如何避免回表查询?什么是索引覆盖
  • 原文地址:https://www.cnblogs.com/arax/p/8525270.html
Copyright © 2020-2023  润新知