SpringMVC 获取国际化信息(JSTL view,java代码中,超链接可以选择的方式)
程序员文章站
2022-05-24 11:17:07
...
国际化:
1.在页面获取:JSTL view
先设置2个国际化资源文件,然后在springmvc.xml中进行配置(resoucebundlemessagesource bean进行配置)
<fmt:message key=“国际化资源文件中对应的key”></fmt:message>
见这个文章中的详细介绍(https://blog.csdn.net/weixin_38626208/article/details/83246877)
2.页面代码
<a href="testGlobal">Test Global</a>
3.Java代码中
@RequestMapping("/testGlobal")
public String testGlobal(HttpServletRequest request){
RequstContext context=new RequstContext(request);
String message=context.getMessage("user.name");//资源文件的Key值
return "success";
}
3.页面当中超链接可以选择的方式
本地化解析器与拦截器
acceptHeaderLocateResolver:根据HTTP请求头的accept-language参数确定本地化的类型,如果没有显式定义本地化解析器,springmvc使用该解析器
-CookieLocalResolver
-SessionLocalResolver:本地化信息存储在session中,根据Session中特定的属性确定本地化类型
-LoaclChangeInteceptor
global页面中代码:
<%@talib="fmt全路径" prefix="fmt"%>
<a href="global?locale=zh_CN">中文</a>
<a href="global?locale=en_US">English</a>//将pageEncoding=UTF-8
<fmt:message key="header"> </fmt:message>//header为国际化资源文件中的key
在springmvc.xml中代码如下:
<bean id="sessionLoalResolver" class="sessionLocalResolver全路径">
<mvc:interceptors>
<bean id="localchangeinterceptor' class="LocalChangeInceptor全路径"</bean>
<mvc:interceptor>
<mvc:mapping path="/toFormat"/>//拦截资源的设定,如果没有mapping就是拦截所有的请求
<bean id="MyInterceptor" class="MyInterceptor包的全路径“”
</mvc:interceptor>
</mvc:interceptors>
**<mvc:view-controller path="/global"view-name="global">**
上一篇: 增加动态验证码
下一篇: thinkphp3.2.3微信支付插件