这里写自定义目录标题
Spring-MVC 国际化实现中英文切换
本文介绍如何使用Spring 搭建国际化中英文切换功能
实例代码
JSP页面
<%@ page import="javax.naming.Context" %><%--
Created by IntelliJ IDEA.
User: jason
Date: 11/27/2020
Time: 9:40 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
pageContext.setAttribute("contextPath", request.getContextPath());
%>
<a href="${contextPath}/i18nLogin">国际化页面登录</a>
</body>
</html>
Controller
package com.mashibing.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Locale;
@Controller
public class I18nLoginController {
@Autowired
private MessageSource messageSource;
// @RequestMapping("/i18nLogin")
// public String i18nTest(HttpServletRequest request,Locale locale){
// System.out.println("i18nLogin.jsp------------------");
// System.out.println("local="+request.getParameter("locale"));
// String username = messageSource.getMessage("username", null, locale);
// System.out.println("username="+username);
// return "i18nLogin";
// }
@RequestMapping("/i18nLogin")
public String i18nTest(@RequestParam(value = "locale",required = false) String localeStr, Locale locale, HttpSession session ){
if(localeStr!=null&&!"".equals(localeStr)){
locale = new Locale(localeStr.split("_")[0],localeStr.split("_")[1]);
}
session.setAttribute(SessionLocaleResolver.class.getName() + ".LOCALE",locale);
System.out.println("local="+locale);
return "i18nLogin";
}
}
Spring MVC 配置文件
<?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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.mashibing"></context:component-scan>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="suffix" value=".jsp"/>
<property name="prefix" value="/WEB-INF/page/"/>
</bean>
<bean class="com.mashibing.view.MyViewResolver">
<property name="order" value="1"></property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="1024000"></property>
</bean>
<mvc:interceptors>
<!-- 直接写在 mvc:interceptors 里的bean 是全局拦截器,如果写的路径了,是符合mapping 路径的url 才会被拦截-->
<!-- <bean class="com.mashibing.interceptor.MyInterceptor1"></bean>-->
<!-- <bean class="com.mashibing.interceptor.MyInterceptor2"></bean>-->
<!-- <bean class="com.mashibing.interceptor.MyInterceptor3"></bean>-->
<mvc:interceptor>
<mvc:mapping path="/test/**"/>
<bean class="com.mashibing.interceptor.MyInterceptor1"></bean>
</mvc:interceptor>
</mvc:interceptors>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="test.login"></property>
</bean>
<!-- 这里必须使用 localResovler-->
<!-- <bean id="localeResolver" class="com.mashibing.Resolver.MylocalResolver" ></bean>-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
</beans>
jsp 页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> % >
<html>
<head>
<title>i18nLogin</title>
</head>
<body>
<%
pageContext.setAttribute("ctp",request.getContextPath());
%>
<h1><fmt:message key="welcomeinfo"/></h1>
<form action="${ctp}/i18nLogin" method="post" >
<fmt:message key="username"/>: <input type="text" name="username"/><br><br>
<fmt:message key="password"/>: <input type="password" name="password"/><br><br>
<input type="submit" value="<fmt:message key="loginBtn"/>"/>
</form>
<a href="${ctp}/i18nLogin?locale=zh_CN">中文</a>
<br>
<a href="${ctp}/i18nLogin?locale=en_US">英文</a>
</body>
</html>
properties 文件
welcomeinfo=welcome to mashibing.com
username=USERNAME
password=PASSWORD
loginBtn=LOGIN
welcomeinfo=欢迎来到马士兵教育
username=用户名少时诵诗书
password=密码
loginBtn=登录