springmvc国际化

来源:互联网 发布:ssh服务默认端口 编辑:程序博客网 时间:2024/05/17 01:38

关于国际化:

1. 在页面上能够根据浏览器语言设置的情况对文本(不是内容), 时间, 数值进行本地化处理

2. 可以在 bean 中获取国际化资源文件Locale 对应的消息

3. 可以通过超链接切换 Locale, 而不再依赖于浏览器的语言设置情况

解决: (都首先要配置springmvc.xml的国际化资源文件)

1. 使用 JSTL 的 fmt 标签

2. 在 bean 中注入ResourceBundleMessageSource 的示例, 使用其对应的getMessage方法

3. 配置 LocalResolver 和 LocaleChangeInterceptor


1. 使用 JSTL 的 fmt 标签

在i18n.jsp中使用 JSTL 的 fmt 标签:<fmt:message key="i18n.user"></fmt:message>

在index.jsp中设置的链接:<a href="i18n">I18N PAGE</a>

在springmvc.xml中:

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

<property name="basename" value="i18n"></property>

</bean>

还可以设置直接跳转到想要的界面,不需要handlers的介入

<mvc:view-controllerpath="/i18n" view-name="i18n"/>

2. 在handlers中

@Autowired

       privateResourceBundleMessageSource messageSource;

@RequestMapping("/i18n")

       publicString testI18n(Locale locale){

              String val = messageSource.getMessage("i18n.user", null, locale);

              System.out.println(val);   

      return "i18n";  

}

3. 在springmvc.xml中配置

  <!-- 配置 SessionLocalResolver -->

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

<!-- 配置 LocaleChanceInterceptor -->

<mvc:interceptors>

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

</mvc:interceptors>

此外的超链接是:

<a href="i18n?locale=zh_CH">中文</a>

       <ahref="i18n?locale=en_US">英文</a>