- 配置springmvc.xml
<!-- 国际化资源文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:message" />
</bean>
<!-- SessionLocalResolver -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>
<!-- LocaleChangeInterceptor -->
<mvc:interceptors>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
- src下创建资源文件
message_en_US.properties
language.cn = Chinese
language.en = English
language.username = username
language.password = password
language.submit = submit
message_zh_CN.properties
language.cn = \u4e2d\u6587
language.en = \u82f1\u6587
language.username = \u7528\u6237\u540D
language.password = \u5BC6\u7801
language.submit = \u63D0\u4EA4
目标方法
@RequestMapping("/testI18n")
public String testI18n(){
return "i18n";
}
前端页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 记得引入spring标签哦 -->
<%@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>Insert title here</title>
</head>
<body>
Language: <a href="?lang=zh_CN"><spring:message code="language.cn" /></a> - <a href="?lang=en_US"><spring:message code="language.en" /></a>
<br>
<form action="#">
<spring:message code="language.username" />:<input type="text" name="username" /><br>
<spring:message code="language.password" />:<input type="text" name="password" /><br>
<button type="submit"><spring:message code="language.submit" /></button>
</form>
<!-- 当点击链接XXX?lang=zh_CN
那么code=luanguage.cn/en用的就都是message_zh_CN.properties文件的信息 -->
</body>
</html>