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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值