springMVC中的message标签
属性 | 描述 |
arguments | 标签的参数,可以是字符串、数组或对象 |
code | 获取消息的key |
text | 如果code不存在,则text为默认显示的文本 |
var | 用于保存消息的变量 |
一、基于浏览器的国际化
1、在src目录下新建两个资源文件,分别代表中、英文
src\message_en_US.properties
loginname = Login name:
password = Password:
submit = Submit
welcome = Welcome {0} to here
title = Login Page
username = administrator
src\message_zh_CN.properties
loginname = 登录名:
password = 密码:
submit = 提交
welcome = 欢迎 {0} 来到这里
title = 登录页面
username = 管理员
2、在WEB-INF/content目录下新建login.jsp页面,使用<spring:message/>标签输出国际化消息
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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>
<!-- 使用message标签来输出国际化信息 -->
<h4><spring:message code="title"/></h4>
<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>
3、在web.xml中配置spring
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<!-- 前端控制器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- contextConfigLocation是参数名称,该参数的值包含的是springmvc的配置文件路径 -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-config.xml</param-value>
</init-param>
<!-- 在web应用程序启动时立即加载Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring的核心文件,必须使用context-param,ContextLoaderListener才能找到 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-config.xml</param-value>
</context-param>
</web-app>
4、在WEB-INF目录的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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- spring自动扫描base-package下面的包或子包下面的java文件 -->
<context:component-scan base-package="kshon.controller,kshon.pojo" />
<!-- 设置配置方案 -->
<mvc:annotation-driven />
<!-- 使用默认的servlet响应静态文件 -->
<mvc:default-servlet-handler/>
<!-- 配置annotation类型的处理映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 配置annotation类型的处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 视图解析器 -->
<bean 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"></property>
</bean>
<!-- 国际化操作拦截器如果采用基于(session/cookie)则必须配置 -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>
</beans>
5、在src/pojo目录下编写User.java类
package kshon.pojo;
import java.util.List;
public class User{
private String loginname;
private String password;
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
6、在src/controller目录下编写UserController.java控制器
package kshon.controller;
import java.util.ArrayList;
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 kshon.pojo.Dept;
import kshon.pojo.User;
@Controller
public class UserController {
public UserController(){
super();
}
@RequestMapping("/loginForm")
public String loginForm(Model model){
User user = new User();
model.addAttribute("user", user);
return "login";
}
@RequestMapping("/login")
public String login(@ModelAttribute User user,Model model,HttpServletRequest request){
if(user.getPassword().equals("123456")){ //密码为123456则登录成功
//从后台代码获取国际化信息的username
RequestContext rContext = new RequestContext(request);
String username = rContext.getMessage("username");
//将获取的username保存到user对象中并设置到model
user.setUsername(username);
model.addAttribute("user", user);
return "welcome";
}
return "login";
}
}
7、最后在WEB-INF/content目录下编写welcome.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>Insert title here</title>
</head>
<body>
<h3><spring:message code="welcome" arguments="${requestScope.user.username }"/></h3>
</body>
</html>
启动tomcat,输入http://localhost:8080/SpringMVC/loginForm
这是中文版的,如果你使用的是chrome浏览器,可以在右上角点击设置——高级——语言——添加语言——输入英语(美国)——移至顶部,这时chrome会使用英文展示页面
浏览器中输入http://localhost:8080/SpringMVC/loginForm
二、基于HttpSession的国际化
1、在WEB-INF/content目录下编写login.jsp页面,由用户自己选择是用中文还是英文
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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>
<a href="login?request_locale=zh_CN">中文</a>|<a href="login?request_locale=en_US">英文</a>
<!-- 使用message标签来输出国际化信息 -->
<h4><spring:message code="title"/></h4>
<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>
2、修改spring配置文件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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- spring自动扫描base-package下面的包或子包下面的java文件 -->
<context:component-scan base-package="kshon.controller,kshon.pojo" />
<!-- 设置配置方案 -->
<mvc:annotation-driven />
<!-- 使用默认的servlet响应静态文件 -->
<mvc:default-servlet-handler/>
<!-- 配置annotation类型的处理映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 配置annotation类型的处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 视图解析器 -->
<bean 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"></property>
</bean>
<!-- 国际化操作拦截器如果采用基于(session/cookie)则必须配置 -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
</beans>
3、在src/controller下面改写UserController.java控制器
package kshon.controller;
import java.util.ArrayList;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.support.RequestContext;
import kshon.pojo.Dept;
import kshon.pojo.User;
@Controller
public class UserController {
public UserController(){
super();
}
@RequestMapping("/login")
public String login(String request_locale,Model model,HttpServletRequest request){
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 "login";
}
}
启动tomcat,输入http://localhost:8080/SpringMVC/login