Spring MVC +i18n实现国际化

第一步:中文和英文properties文件


第二步:配置SpringMVC的I18N支持

<!-- 国际化资源配置,资源文件绑定器 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- 国际化资源文件配置,指定properties文件存放位置 -->
<property name="basename" value="classpath:/message" />
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<!-- 动态切换国际化 ,国际化放在session中 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<!-- 默认中文 -->
<property name="defaultLocale" value="zh_CN"/>
</bean>
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<!-- 通过这个参数来决定获取那个配置文件 -->
<property name="paramName" value="language" />
</bean>
</mvc:interceptors>


第三步:使用例子

@RequestMapping(value = "/index.do", method = { RequestMethod.GET, RequestMethod.HEAD })
public ModelAndView selectDataToIndex(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> jsonResult = new HashMap<String, Object>();
jsonResult.put("success", Boolean.TRUE);
RequestContext req = new RequestContext(request);
jsonResult.put("username", req.getMessage("username"));
jsonResult.put("password", req.getMessage("password"));
ModelAndView modelAndView = new ModelAndView("index", jsonResult);

return modelAndView;
}

前端页面


最后大功告成就实现了










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值