第一步:中文和英文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;
}
前端頁面
最後大功告成就實現了