• 八、springMVC之国际化


     一、关于国际化使用场景:

    1.在页面上能够根据浏览器语言设置的情况对文本(不是内容),时间,数值进行本地化处理,显示对应语言的版本;

    2.可以在bean中获取国际化资源文件LOCAL对应的消息,即在bean中获取当前消息,对应的语言版本;

    3.可以通过超链接切换locale,而不再依赖浏览器的语言设置情况

    二、场景实现 

    1.目录结构:

    2.配置

    web.xml:只配置了springDispatcherServlet;

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     5     id="WebApp_ID" version="3.1">
     6     <servlet>
     7         <servlet-name>springDispatcherServlet</servlet-name>
     8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     9         <!-- 配置DispatcherServletd 一个初始化参数:配置springmvc配置文件的位置和名称 -->
    10         <!-- 实际上也可以不通过 contextConfigLocation 来配置Springmvc的配置文件,而是用默认的 即默认的配置文件为 
    11             /WEB-INF/<servlet-name>-servlet.xml 本项目默认位置配置文件即为: /WEB-INF/springDispatcherServlet-servlet.xml -->
    12         <init-param>
    13             <param-name>contextConfigLocation</param-name>
    14             <param-value>classpath:spring.xml</param-value>
    15         </init-param>
    16         <!-- 表示springDispatcherServlet在加载的时候被创建 -->
    17         <load-on-startup>1</load-on-startup>
    18     </servlet>
    19 
    20     <!-- Map all requests to the DispatcherServlet for handling -->
    21     <servlet-mapping>
    22         <servlet-name>springDispatcherServlet</servlet-name>
    23         <url-pattern>/</url-pattern>
    24     </servlet-mapping>
    25 </web-app>
    View Code

    spring.xml基础配置(适用于场景1、2):

    包扫描的配置、视图解析器配置,国际化资源文件配置、<mvc:annotation-driven></mvc:annotation-driven>;

     1 <context:component-scan base-package="handler"></context:component-scan>
     2     <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
     3     <bean
     4         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     5         <property name="prefix" value="/"></property>
     6         <property name="suffix" value=".jsp"></property>
     7     </bean>
     8     
     9     <bean id="messageSource"
    10         class="org.springframework.context.support.ResourceBundleMessageSource">
    11         <property name="basename" value="message"></property>
    12     </bean>
    13     
    14     <mvc:annotation-driven></mvc:annotation-driven>
    View Code

    spring.xml配置(适用于场景3):

    实现 通过超链接切换locale,而不再依赖浏览器的语言设置,需要配置

    1 <!-- 配置SessionLocalResolver(locale切换语言配置1) -->
    2     <bean id="localeResolver"
    3         class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
    4     <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 --> 
    5     <mvc:interceptors>
    6         <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    7     </mvc:interceptors>

    完整配置为:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:mvc="http://www.springframework.org/schema/mvc"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
     7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     9 
    10     <context:component-scan base-package="handler"></context:component-scan>
    11     <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
    12     <bean
    13         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    14         <property name="prefix" value="/"></property>
    15         <property name="suffix" value=".jsp"></property>
    16     </bean>
    17     
    18     <bean id="messageSource"
    19         class="org.springframework.context.support.ResourceBundleMessageSource">
    20         <property name="basename" value="message"></property>
    21     </bean>
    22     
    23     <mvc:annotation-driven></mvc:annotation-driven>
    24     
    25     <!-- 配置SessionLocalResolver(locale切换语言配置1) -->
    26     <bean id="localeResolver"
    27         class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
    28     <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 --> 
    29     <mvc:interceptors>
    30         <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    31     </mvc:interceptors>
    32     
    33 </beans>
    View Code

    3.国际化文件message_en_US.properties&message_zh_CN.properties

    1 I18N.TEST_EN_US=TEST_EN_US
    I18N.TEST_EN_US = 测试语言

    4.EmployeeController.java

     1 package handler;
     2 
     3 import java.util.Locale;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.context.support.ResourceBundleMessageSource;
     7 import org.springframework.stereotype.Controller;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 
    10 @RequestMapping("/emp")
    11 @Controller
    12 public class EmployeeController {
    13     @Autowired
    14     private ResourceBundleMessageSource messageSource;
    15 
    16     @RequestMapping("/testLang1")
    17     public String testLang1() {
    18         return "forward:/index.jsp";
    19     }
    20 
    21     @RequestMapping("/testLang")
    22     public String testLang(Locale locale) {
    23         String val = messageSource.getMessage("I18N.TEST_EN_US", null, locale);
    24         System.out.println(val);
    25         return "forward:/index.jsp";
    26     }
    27 
    28 }

    5.index.jsp

    分别使用 fmt 和spring tag;两种标签实现的结果是一致的,可以任意选择一种;

    场景1、2测试页面;

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@page import="java.util.*"%>
     4 <%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
     5 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
     6 <!DOCTYPE html>
     7 <html>
     8 <head>
     9 <meta charset="UTF-8">
    10 <title>Insert title here</title>
    11 </head>
    12 <body>
    13 <p>    
    14     <fmt:bundle basename="message">
    15         <fmt:message key="I18N.TEST_EN_US"></fmt:message>
    16     </fmt:bundle>
    17     </p>    
    18     
    19     <p><s:message code="I18N.TEST_EN_US"></s:message></p>
    20     
    21 </body>
    22 </html>
    View Code

    场景3的测试页面:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@page import="java.util.*"%>
     4 <%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
     5 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
     6 <!DOCTYPE html>
     7 <html>
     8 <head>
     9 <meta charset="UTF-8">
    10 <title>Insert title here</title>
    11 </head>
    12 <body>
    13 <p>    
    14     <fmt:setLocale value="${param.locale}"/>
    15     <fmt:bundle basename="message">
    16         <fmt:message key="I18N.TEST_EN_US"></fmt:message>
    17     </fmt:bundle>
    18     </p>    
    19     
    20     <p><s:message code="I18N.TEST_EN_US"></s:message></p>
    21     
    22 </body>
    23 </html>

    运行结果:

    场景1,2的配置方式下的运行结果:

    访问:http://localhost:8080/DataOperate/emp/testLang1,此时chrome浏览器语言为中文,测试页面显示中文,如下图且控制台;语言切换到英语时,显示为英语;,显示如下图;

    访问:http://localhost:8080/DataOperate/emp/testLang,此时chrome浏览器语言为中文,测试页面显示中文且控制台TEST_EN_US,语言切换到英语时,显示为英语,且控制台打印:测试语言;

    语言为中文的测试效果图  语言设置为中文

    语言为英语的测试效果图  语言设置为英语

    场景3的配置方式下的运行结果:

     访问http://localhost:8080/DataOperate/emp/testLang?locale=en_US 页面显示英文;访问http://localhost:8080/DataOperate/emp/testLang?locale=zh_CN 页面显示中文;

      

     

     

     

  • 相关阅读:
    DevOps
    DevOps
    微信的NATIVE支付提示201商户订单号重复的解决方案
    phpstorm 破解
    Git忽略已经被版本控制的文件(添加.gitignore不会起作用)
    微信 {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
    如何用AJax提交name[]数组?
    基于PHP给大家讲解防刷票的一些技巧
    为何GET只发一次TCP连接,POST发两次TCP连接
    IP地址在mysql的存储(IP地址和int的转换)
  • 原文地址:https://www.cnblogs.com/lixiuming521125/p/16021546.html
Copyright © 2020-2023  润新知