SSM笔记-SpringMVC的语言国际化

1、步骤
①创建国际化资源文件
②springmvc配置文件中配置国际化资源文件
③如果需要通过按钮更换语言locale时, springmvc配置文件中配置SessionLocalResolver和localchannceinterceptor
④创建Handler类并且定义ResourceBundleMessageSource对象,用@Autowired修饰该对象
⑤如果handler方法需要获取对应语言的文字的时候, 入参添加一个Locale类型的数据, 并且在方法中使用ResourceBundleMessageSource.getMessage(key, null, Locale)获取对应的文字
⑥在页面添加taglib标签prefix为”fmt”, uri为”http://java.sun.com/jsp/jstl/fmt
⑦使用fmt:message在页面显示文字
⑧通过action?locale=en_US的方式做切换语言按钮

2、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>SpringMVC_11_MutileLanguage</display-name>
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

3、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: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.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

    <!-- 配置自动扫描包 -->
    <context:component-scan base-package="com.test.springmvc"></context:component-scan>

    <!-- 配置视图解析器,把handler返回值解析为实际视图 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 配置国际化资源文件 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>
    </bean>

    <!-- 以下配置是用于通过按钮更换语言locale时,说需要用到的 -->
    <!-- 配置SessionLocalResolver -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
    <!-- 配置localchannceinterceptor -->
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
    </mvc:interceptors>

    <!-- 配置跳转连接的方法,适用于不希望页面名字显示出来的一般的页面之间跳转 -->
    <mvc:view-controller path="/index" view-name="index"/>

</beans>

4、Handler.java

package com.test.springmvc.handlers;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Handler {

    @Autowired
    private ResourceBundleMessageSource messageSource;

    @RequestMapping("/index2")
    public String testI18n(Locale locale) {
        String name = messageSource.getMessage("name", null, locale);
        String password = messageSource.getMessage("password", null, locale);
        String description = messageSource.getMessage("description", null, locale);
        System.out.println(name);
        System.out.println(password);
        System.out.println(description);
        return "index2";
    }
}

5、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="index2">index2</a><br><br>
</body>
</html>

6、index2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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="index">index</a><br>
    <a href="index2?locale=en_US">EN</a><br>
    <a href="index2?locale=zh_CN">CN</a><br><br>

    <fmt:message key="name"></fmt:message><br>
    <fmt:message key="password"></fmt:message><br>
    <fmt:message key="description"></fmt:message><br>

</body>
</html>

7、i18n.properties

name=name!
password=password!
description=description!

8、i18n_en_US.properties

name=name!
password=password!
description=description!

9、i18n_zh_CN.properties

name=\u540D\u5B57!
password=\u5BC6\u7801!
description=\u63CF\u8FF0!

10、项目目录
项目目录

11、demo
https://download.csdn.net/download/qq_22778717/10601218

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值