第一步:中文和英文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;
}
前端页面
最后大功告成就实现了