示例:基于浏览器请求的国际化实现
创建资源文件message_en_US.properties
title=login page
userName=userName:
passWord=passWord:
submit=submit
welcom=welcom {0}
name=administrator
创建资源文件message_zh_CN.properties
title=登录
userName=用户名:
passWord=密码:
submit=提交
welcom=欢迎 {0}
name=管理员
创建login.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>
<html>
<head>
<meta charset="utf-8">
<title><spring:message code="title"></spring:message></title>
</head>
<body>
<form:form method="post" action="login">
<table>
<tr>
<td><spring:message code="userName"></spring:message></td>
<td><form:input path="userName"/></td>
</tr>
<tr>
<td><spring:message code="passWord"></spring:message></td>
<td><form:password path="passWord"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="<spring:message code="submit"></spring:message>"></td>
</tr>
</table>
</form:form>
</body>
</html>
接下来在springmvc-config.xml配置文件中加载国际化资源文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 使用注解驱动 -->
<mvc:annotation-driven />
<!-- 定义扫描的包 -->
<context:component-scan base-package="com.*" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value=""></property>
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:basenames="message"/>
</beans>
创建UserController
package com.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;
import com.po.User;
@Controller
public class UserController {
@RequestMapping("/loginForm")
public String loginForm(Model model) {
User user=new User();
model.addAttribute("command",user);
return "login";
}
@RequestMapping("/login")
public String login(@ModelAttribute User user,Model model,
HttpServletRequest request) {
if(user.getUserName()!=null&&user.getUserName().equals("admin")&&
user.getPassWord()!=null&&user.getPassWord().equals("123")) {
RequestContext requestContext=new RequestContext(request);
String name=requestContext.getMessage("name");
user.setName(name);
model.addAttribute("user",user);
return "success";
}return "error";
}
}
创建success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>success</title>
</head>
<body>
<spring:message code="welcom" arguments="${requestScope.user.name }"></spring:message>
</body>
</html>
启动Tomcat访问http://localhost:8080/spring/loginForm(默认语言:简体中文)
切换浏览器语言(英语)
输入正确用户名(admin)和密码(123)提交表单
切换浏览器语言(英语)