如何实现 Spring MVC i18n 国际化,动态设置默认语言

如何实现 Spring MVC i18n 国际化,动态设置默认语言,第1张

1.在spring配置文件中配置资源文件properties的位置及公共名,下列配置指定的properties文件处于src目录下的resources文件夹中,名字为message_info_*.properties。

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

<propertyname="basenames">

<list>

<value>resources/message_info</value>

</list>

</property>

<propertyname="useCodeAsDefaultMessage"value="true"/><!--

Set whether to usethe message code as default message instead of

throwing aNoSuchMessageException. Useful for development

anddebugging. -->

</bean>

2.在spring配置文件中配置基于session的处理,将提交上来的locale参数进行处理,下列代码默认加载的语言是中文简体。

<bean id="localeResolver"class="org.springframework.web.servlet.i18n.SessionLocaleResolver">

<propertyname="defaultLocale"value="zh_CN"></property>

</bean>

3.在spring配置文件中的controller内配置相应的拦截器。

<beanid="className"

class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">

<propertyname="interceptors">

<list>

<beanclass="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>

</list>

</property>

</bean>

4.相应的properties文件内写入对应的语言,配置文件的语言信息以keyvalue的形式进行存储。

5.利用jstl的fmt标签库进行相应数据的国际化。

1)导入相应的fmt标签库<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

2)需要国际化处写入<fmt:messagekey="title"></fmt:message>标签,此处将显示相应properties文件中名为title的信息。

3)页面上写成三个连接用于控制国际化的转换

<ahref="/CloudPortal/staff/goindex.do?locale=zh_CN">Chinese</a>//message_info_zh_CN.properties

<ahref="/CloudPortal/staff/goindex.do?locale=en_US">English</a>//message_info_en_US.properties

<ahref="/CloudPortal/staff/goindex.do?locale=zh_TW">Chinese(TW)</a>//message_info_zh_TW.properties

locale内部固定的参数用于判断读取请求的配置文件。

-------------------------------------------------------------

分割线--------------2014-11-11 by

zhang------------------------------------------------------------------------------------------------------

springmvc如何实现国际化

1.springmvc实现国际化有多种方式(自行百度)。

几种方式无非就是配置国际化方式和读取国际化资源文件从而实现国际化,下面本文介绍基于session的国际化配置,感觉配置比较方便快捷一些。

(1).首先配置配置国际化在spring-servlet.xml,即springmvc的配置文件中(xxx-servlet.xml)。

<!-- springmvc 国际化配置 (基于Session的国际化配置 方式)-->

<bean id="localeResolver"

class="org.springframework.web.servlet.i18n.SessionLocaleResolver">

</bean>

<bean id="messageSource"

class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

<property name="fallbackToSystemLocale">

<value>false</value>

</property>

<property name="basename" value="/WEB-INF/i18n/resources/messages" />

</bean>

(2)在WEB-INF文件夹下按照

[html] view plaincopy

/WEB-INF/i18n/resources/messages

配置新建文件夹,在文件夹里面新建配置文件

messages_zh_CN.properties(中文国际化资源文件),messages.properties(英文国际化资源文件)国际化资源文件。

到此配置完毕。

注:springmvc的国际化必须要经过控制器Controller才能生效。

2.如何在jsp页面读取国际化

(1)引入<spring:message/>标签如:<spring:message key=''user.manage.role/>

或者

(2)引入<fmt:message/>标签例如<fmt:message key='user.manage.role'/>,其中user.manage,role在messages_zh_CN.properties中配置为:user.manage.role=\u89D2\u8272,在messages.properties中配置为user.manage.role=Role,即可。

一、基于浏览器语言的国际化配置 使用Spring的MVC,并且配置中有配置Resource文件 Xml代码 以下是引用片段: 其中,message-info是你的properties文件的通用名。如:我的配置文件叫 message-info.properties,message-info_zh_CN.properties等等,只要有了这个配置,然后配置JSP 渲染器为JSTL支持的,那么在你的JSP文件中使用fmt标记就可以实现客户浏览器语言国际化了。 如: 以下是引用片段: 其中的info.login.title和你的资源文件对应. 另外一种方式是使用spring自带的标签显示国际化信息,如: 以下是引用片段:

"/>

二、基于动态加载的国际化配置 1、基于请求的国际化配置 基于请求的国际化配置是指,在当前请求内,国际化配置生效,否则自动以浏览器为主。 配置方式如下: 首先配置拦截器 以下是引用片段: 这个配置呢,是不论请求级别的国际化,还是Cookie级别的国际化,再或者Session级别的国际化,都必需有配置这个拦截器,否则会不能使用。 配好上面的拦截器之后,就将拦截器注入到你的UrlHandlerMapping中,例如: Xml代码 以下是引用片段: 1 这个时候,但凡有了符合UrlMapping的请求,就会被拦截,并且开始配置国际化参数 以下是引用片段: 默认的参数名为locale主意大小写。里面放的就是你的提交参数。如:en_US,zh_CN之类的,这个时候,你在页面上加一句简体中文 如果你的资源中,饱含建议中文的配置,那么就会变成你确定的简体中文拉。 2、基于Session的国际化配置 拦截器和基于请求的相同 Session的配置如下: 以下是引用片段: 在你的处理的Controller中,将提交上来的locale字段信息生成真正的Locale对象,然后将对象保存在Session中,默认保存的ID是SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME 这样,当你的Session不过期,那么语言种类始终保持正确的说。我一直是这样子用的,我觉得还是Session的好,老外们用了很满意。 3、基于Cookie的国际化配置 这个我就不说了,反正用的不多,至少我做的项目能不用Cookie就不用Cookie,所以,基于Cookie的国际化配置我就不细说了,如果想知道怎么配置,那么下载一个Spring,其中的例子程序就是用Cookie配置的,你自己读代码就OK了。 三、注意事项 如果不用默认的浏览器语言国际化方式,那么拦截器一定要配置,如果你有多个UrlMapping,那么就每个都配上拦截器。 至于配置的LocaleResolver的名字,一定要用上面的配置中的名字localeResolver当然了,这个是默认的名字来的,自己设置成别的也可以,但是就是麻烦,反正我用默认的就感觉不错 解决问题: 在前几天引用“Spring的MVC I18N-国际化相关配置 ”并做了测试,发现 有一问题。程序运行会抛出异常 “Cannot change HTTP accept header - use a different locale resolution strategy”,根本原因是spring source 做了限制,源码如下 Java代码 以下是引用片段: public class AcceptHeaderLocaleResolver implements LocaleResolver { public Locale resolveLocale(HttpServletRequest request) { return request.getLocale()} public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { throw new UnsupportedOperationException( "Cannot change HTTP accept header - use a different locale resolution strategy")} } 请注意上面的类,该类允许继承,所以需要改写setLocale方法,源码示范如下 Java代码 以下是引用片段: package org.springframework.web.servlet.i18nimport java.util.Localeimport javax.servlet.http.HttpServletRequestimport javax.servlet.http.HttpServletResponseimport org.springframework.web.servlet.DispatcherServletimport org.springframework.web.servlet.LocaleResolverpublic class MyAcceptHeaderLocaleResolver extends AcceptHeaderLocaleResolver { private Locale myLocalpublic Locale resolveLocale(HttpServletRequest request) { return myLocal} public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { myLocal = locale} } 然后在action-servlet.xml里的设置为 Xml代码 以下是引用片段: chinese.do=filenameController us.do=filenameController

SpringMVC的@ResponseBody返回中文乱码的原因是SpringMVC默认处理的字符集是ISO-8859-1,在Spring的org.springframework.http.converter.StringHttpMessageConverter类中可以看到如下代码:

public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1")

解决返回中文乱码的问题有两种,第一种是局部的,只针对于某个方法的返回进行处理,第二种是全局的,针对于整个项目,如下:

第一种:在@RequestMapping中添加produces="text/htmlcharset=UTF-8,如:

[html] view plaincopy

@RequestMapping(value="/login.do",method=RequestMethod.POST,produces="text/htmlcharset=UTF-8")

@ResponseBody

public String login(@RequestParam(value="username") String userName,@RequestParam(value="password") String password){

return JSONMessageUtil.getSuccessJSON("登录成功")

}

第二种:在配置文件中的mvc:annotation-driven中添加如下代码:

[html] view plaincopy

<mvc:annotation-driven >

<!-- 消息转换器 -->

<mvc:message-converters register-defaults="true">

<bean class="org.springframework.http.converter.StringHttpMessageConverter">

<property name="supportedMediaTypes" value="text/htmlcharset=UTF-8"/>

</bean>

</mvc:message-converters>

</mvc:annotation-driven>

<mvc:resources location="/resources/" mapping="/resources/**" />


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11379132.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存