SpringMVC 坑路11 -> 国际化

32 篇文章 0 订阅
13 篇文章 1 订阅

静态国际化配置

动态国际化配置

请求级别的国际化处理

Session 级别的国家化处理

Cookie 级别的国际化处理

静态国际化配置

1、在 Spring 的配置文件里加入

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMEssageSource">
    <!-- welcomes 为资源文件的前缀 -->
    <property name="basenames" value="messages"/>
    <property name="defaultEncoding" value="utf-8"/>
</bean>

注意:messageSource 不能随意更改名称。

2、配置资源文件

classpath:message_zh_CN.properties
classpath:message_en_US.properties

3、在 JSP 页面使用
1、使用 Spring 的标签实现

<spring:message code="welcome.springmvc" text="default text"/>

2、使用 JSTL 标签实现

<fmt:message key="welcome.springmvc"/>

动态国际化配置

动态国际化包含:
1、请求级别的国际化处理
2、Cookie 级别的国际化处理
3、Session 级别的国际化处理

需要在 Spring 的配置文件中加入

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>

请求级别的国际化处理

在请求的 URL 中传入参数 locale
例如:locale=zn_CN
如果想修改请求参数名,则配置如下

<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="myName"/>
</bean>

Spring 配置文件中加入配置

<!-- 基于 Cookie 的本地化解析器 -->
<bean id="localResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="cookieMaxAge" value="604800"/>
    <property name="defaultLocale" value="zh_CN"/>
    <property name="cookieName" value="Language"></property>
</bean>

在 Controller 中加入

@Autowired SessionLocaleResolver resolver;

在方法中加入

resolver.setLocale(request,response,Local.XX); 

Session 级别的国际化处理

Spring 配置文件中加入配置:

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

在 Controller 中加入

@Autowired SessionLocaleResolver resolver;

在方法中加入

resolver.setLocale(request,response,Local.XX); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值