SpringMVC 国际化
1. messageSource
接口
1.1 多组属性文件配置
<!-- 国际化 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message</value>
<value>testInfo</value>
</list>
</property>
</bean>
1.2 单组属性文件配置
<!-- 国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化资源文件名 -->
<property name="basenames" value="message"/>
</bean>
2. localeResolver
语言区域解析器接口
实现类都在
org.springframework.web.servlet.i18n
包中,包括:
AcceptHeaderLocaleResolver
,默认,读取浏览器accept-language
标题来确定使用语言区域。可以不用哪个显示配置。SessionLocaleResolver
CookieLocaleResolver
3. message
标签
在jsp中引入Spring标签库:
<%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>
-
message
标签属性:
属性 | 描述 |
---|---|
arguments |
标签参数,可以是一个字符串、数组或 |
argumentSeparator |
用来分隔该标签的字符 |
code |
获取消息的key |
htmlEscape |
booleam值,表示被渲染的值是否应该进行HTML转义 |
javaScriptEscape |
booleam值,表示被渲染的值是否应该进行JavaScript转义 |
message |
MessageSourceresolvable参数 |
scope |
保存var属性中定义的变量的作用 |
text |
如果code属性不存在,所显示的默认文本 |
var |
用于保存消息的变量 |
4.AcceptHeaderLocaleResolver
国际化
4.1 springmvc配置
<context:component-scan base-package="com.zhougl"/>
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix">
<value>/WEB-INF/content/</value>
</property>
<!-- 后缀 -->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- 国际化 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化资源文件名 -->
<property name="basenames" value="message"/>
</bean>
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(Session/Cookie)则必需配置 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- AcceptHeaderLocaleResolver 配置,因为AcceptHeaderLocaleResolver是默认语言区域解析器,不配置也可以 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver" />
4.2 资源文件
message_zh_CN.properties
loginname= \u767B\u5F55\u540D:
password = \u5BC6\u7801:
submit = \u63D0\u4EA4
welcome = \u6B22\u8FCE {0} \u8BBF\u95EE\u4E66\u9038\u81EA\u5982
title = \u767B\u5F55\u9875\u9762
username = \u7BA1\u7406\u5458
message_en_US.properties
loginname= Login name:
password = Password:
submit = Submit
welcome = Welcom {0} access zhougl
title = Login Page
username = administrator
4.3 Controller层
@Controller
public class UserController{
private static final Log logger = LogFactory.getLog(UserController.class);
@RequestMapping(value="/{formName}")
public String loginForm(
@PathVariable String formName,
Model model){
User user = new User();
model.addAttribute("user",user);
// 动态跳转页面
return formName;
}
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(
@ModelAttribute @Validated User user,
Model model,
HttpServletRequest request) {
logger.info(user);
// 如果登录名是zhougl,密码是123456,则验证通过
if(user.getLoginname()!=null && user.getLoginname().equals("zhougl")
&& user.getPassword()!=null && user.getPassword().equals("123456")){
//从后台代码获取国际化信息username
RequestContext requestContext = new RequestContext(request);
String username = requestContext.getMessage("username");
// 将获取的username信息设置到User对象并设置到Model当中
user.setUsername(username);
model.addAttribute("user", user);
return "success";
}
return "error";
}
}
4.4 jsp
4.4.1 loginForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix= "form" uri= "http://www.springframework.org/tags/form" %>
<%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试基于浏览器请求的国际化</title>
</head>
<body>
<!-- 使用message标签来输出国际化信息 -->
<h3><spring:message code="title"/></h3>
<form:form modelAttribute="user" method="post" action="login" >
<table>
<tr>
<td><spring:message code="loginname"/></td>
<td><form:input path="loginname"/></td>
</tr>
<tr>
<td><spring:message code="password"/></td>
<td><form:input path="password"/></td>
</tr>
<tr>
<td><input type="submit" value="<spring:message code="submit"/>"/></td>
</tr>
</table>
</form:form>
</body>
</html>
4.4.2 success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试基于浏览器请求的国际化</title>
</head>
<body>
<spring:message code="welcome" arguments="${requestScope.user.username}"/>
</body
测试:在设置浏览器语言即可切换语言
5.SessionLocaleResolver
国际化
5.1 Springmvc配置
-
SessionLocaleResolver
语言解析器配置
<!-- 国际化 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message</value>
</list>
</property>
</bean>
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(Session/Cookie)则必需配置 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- SessionLocaleResolver 配置 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
5.2 Controller层
- 基于
HttpSession
@Controller
public class UserController{
private static final Log logger = LogFactory.getLog(UserController.class);
@RequestMapping(value="/{formName}")
public String loginForm(
@PathVariable String formName,
String request_locale,
Model model,
HttpServletRequest request){
System.out.println("request_locale = " + request_locale);
if(request_locale != null){
// 设置中文环境
if(request_locale.equals("zh_CN")){
Locale locale = new Locale("zh", "CN");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
// 设置英文环境
else if(request_locale.equals("en_US")){
Locale locale = new Locale("en", "US");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
// 使用之前的语言环境
else {
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
}
}
User user = new User();
model.addAttribute("user",user);
// 动态跳转页面
return formName;
}
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(
@ModelAttribute @Validated User user,
Model model,
HttpServletRequest request) {
logger.info(user);
if(user.getLoginname()!=null && user.getLoginname().equals("zhougl")
&& user.getPassword()!=null && user.getPassword().equals("123456")){
//从后台代码获取国际化信息
RequestContext requestContext = new RequestContext(request);
String username = requestContext.getMessage("username");
user.setUsername(username);
model.addAttribute("user", user);
return "success";
}
return "error";
}
}
5.3 loginForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix= "form" uri= "http://www.springframework.org/tags/form" %>
<%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试基于SessionLocaleResolver的国际化</title>
</head>
<body>
<!-- 用户可以选择切换语言环境 -->
<a href="loginForm?request_locale=zh_CN">中文</a> | <a href="loginForm?request_locale=en_US">英文</a>
<br/>
<h3><spring:message code="title"/></h3>
<form:form modelAttribute="user" method="post" action="login" >
<table>
<tr>
<td><spring:message code="loginname"/></td>
<td><form:input path="loginname"/></td>
</tr>
<tr>
<td><spring:message code="password"/></td>
<td><form:input path="password"/></td>
</tr>
<tr>
<td><input type="submit" value="<spring:message code="submit"/>"/></td>
</tr>
</table>
</form:form>
</body>
</html>
其余文件与 4小节相同。
5.CookieLocaleResolver
国际化
5.1 Springmvc配置
-
CookieLocaleResolver
语言解析器配置
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
5.2 Controller层
- 基于
Cookie
@Controller
public class UserController{
private static final Log logger = LogFactory.getLog(UserController.class);
@RequestMapping(value="/{formName}")
public String loginForm(
@PathVariable String formName,
String request_locale,
Model model,
HttpServletRequest request){
System.out.println("request_locale = " + request_locale);
if(request_locale != null){
// 设置中文环境
if(request_locale.equals("zh_CN")){
Locale locale = new Locale("zh", "CN");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
// 设置英文环境
else if(request_locale.equals("en_US")){
Locale locale = new Locale("en", "US");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
// 使用之前的语言环境
else {
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
}
}
User user = new User();
model.addAttribute("user",user);
// 动态跳转页面
return formName;
}
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(
@ModelAttribute @Validated User user,
Model model,
HttpServletRequest request) {
logger.info(user);
if(user.getLoginname()!=null && user.getLoginname().equals("zhougl")
&& user.getPassword()!=null && user.getPassword().equals("123456")){
//从后台代码获取国际化信息
RequestContext requestContext = new RequestContext(request);
String username = requestContext.getMessage("username");
user.setUsername(username);
model.addAttribute("user", user);
return "success";
}
return "error";
}
}
其余文件与 4小节相同。
网友评论