SpringMVC国际化配置

一、什么是国际化:

国际化是设计软件应用的过程中应用被使用与不同语言和地区
  国际化通常采用多属性文件的方式解决,每个属性文件保存一种语言的文字信息,
  不同语言的用户看到的是不同的内容

二、springmvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.ssm.controller" />
    <mvc:annotation-driven />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 存储区域设置信息 SessionLocaleResolver类通过一个预定义会话名将区域化信息存储在会话中 从session判断用户语言defaultLocale 
        :默认语言 -->
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="zh_CN" />
    </bean>

    <!-- 国际化资源文件 messageSource配置的是国际化资源文件的路径, classpath:messages指的是classpath路径下的 
        messages_zh_CN.properties文件和messages_en_US.properties文件 设置“useCodeAsDefaultMessage”,默认为false,这样当Spring在ResourceBundle中找不到messageKey的话,就抛出NoSuchMessageException, 
        把它设置为True,则找不到不会抛出异常,而是使用messageKey作为返回值。 -->
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="defaultEncoding" value="UTF-8" />
        <property name="useCodeAsDefaultMessage" value="true" />
        <property name="basenames">
            <list>
                <value>classpath:resource/message</value>
            </list>
        </property>
    </bean>
    <!--该拦截器通过名为”lang”的参数来拦截HTTP请求,使其重新设置页面的区域化信息 -->
    <mvc:interceptors>
        <bean id="localeChangeInterceptor"
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang" />
        </bean>
    </mvc:interceptors>
</beans>

三、国际化语言属性文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rLqwmFkn-1573021959052)(http://note.youdao.com/yws/res/27768/9DA4E29193DB432BAD6D741492F70DC6)]
我的路径是这样,其他的都可以

  1. message_en_US.properties
TITLE = BEGIN TO START
USERNAME = UserName
PASSWORD = PassWord
LOGIN = Login
  1. message_zh_CN.properties
TITLE = 开始冒险之旅
USERNAME = 账号:
PASSWORD = 密码:
LOGIN = 登录

四、编写Controller

@Controller
public class NewsController {
    
    @RequestMapping(value = "/getLogin.html")
    public String getLogin(){
        return "result";
    }
}

五、前端编写并测试

  1. 文件位置

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u0AEv4jY-1573021959052)(http://note.youdao.com/yws/res/27779/1D3A48C8D33744218BB1A63B50B8491C)]
2. index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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="getLogin.html?lang=zh_CN">中文</a>
<br />
<a href="getLogin.html?lang=en_US">英文</a>
</body>
</html>
  1. result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!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>
   <div class="login">
      <h1><spring:message code="TITLE" /> </h1>
      <form action="test.do" method="post">
         <input type="text" name="name" placeholder=<spring:message code="USERNAME" /> required="required" value="" />
         <input type="password" name="password" placeholder=<spring:message code="PASSWORD" /> required="required" value="" />
         <button type="submit" class="btn btn-primary btn-block btn-large"><spring:message code="LOGIN" /> </button>
      </form>
   </div>
</body>
</html>
  1. 测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4WJRyX3c-1573021959053)(https://note.youdao.com/src/09E2F5E6D90D4D24BE032057116D8D43)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Jd9GHJ1C-1573021959053)(http://note.youdao.com/yws/res/27788/6F47DE07348F490EAF69360C82B4100A)]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值