5.2 Spring MVC的国际化处理 5.2.2 基于 Httpsession的国际化
SessionLocaleResolver
SessionLocaleResolver
不是默认的语言区琙解析器,需要对其进行显式配置。如果使用它,Spring MVC
会从HttpSession
作用域中获取用户所设置的语言区域,来确定使用哪个语言区域。
实例 基于Httpsession的国际化实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <%@ 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 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>
|
在loginForm.jsp
页面的最上面增加了两个超链接,分别用于切换中文和英文语言环境。注意请求后面的参数request_locale
,如果请求中文环境则传递的值是”zh_CN
“,如果请求英文环境则传递的值是”en_US
“.
springmvc-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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.3.xsd">
<context:component-scan base-package="org.fkit"/> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/content/" p:suffix=".jsp"/> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basenames="message" p:defaultEncoding="utf-8"/> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> </beans>
|
国际化配置
1 2 3 4 5 6 7 8 9 10 11
|
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basenames="message" p:defaultEncoding="utf-8"/>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
|
UserController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| @Controller public class UserController { @RequestMapping(value = "/loginForm") public String loginForm(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 "loginForm"; } @PostMapping(value = "/login") public String login(@ModelAttribute User user, Model model, HttpServletRequest request) { if (user.getLoginname() != null && user.getLoginname().equals("xiaoming") && 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"; } }
|
loginForm
方法根据提交的 request_ locale
参数值,获取 Session
对象,并调用setAttribute
方法进行语言环境切换。
测试
进入登陆表单,默认显示英文.如下图所示:
切换中文环境
点击中文链接,切换中文环境.如下图所示:
填写账户密码,登陆,效果如下图:
切换英文环境
填写表单:
登陆效果: