在SpringMVC框架下实现数据的国际化(即数据实现多国文字之间的转换)

在eclipse中javaEE环境下:导入必要的架包

web.xml配置文件;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
  
     <!-- 配置SpringMVC的DispatcherServlet -->
    <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>
  
    <!-- 配置 HiddenHttpMethodFilter: 把 POST 请求转为 DELETE、PUT 请求 -->
      <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>

 

spring的bean的配置文件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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.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.0.xsd">
    
    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>
    
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!--  
        default-servlet-handler 将在 SpringMVC 上下文中定义一个 DefaultServletHttpRequestHandler,
        它会对进入 DispatcherServlet 的请求进行筛查, 如果发现是没有经过映射的请求, 就将该请求交由 WEB 应用服务器默认的 
        Servlet 处理. 如果不是静态资源的请求,才由 DispatcherServlet 继续处理

        一般 WEB 应用服务器默认的 Servlet 的名称都是 default.
        若所使用的 WEB 服务器的默认 Servlet 名称不是 default,则需要通过 default-servlet-name 属性显式指定
        
    -->
    <mvc:default-servlet-handler/>
    
    <!-- 一般都会配置这个 <mvc:annotation-driven ></mvc:annotation-driven>,
    由于。。。requestmapping请求实现不了,使用这个,会使requestmapping请求一定实现
    -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 配置国际化资源文件 -->
    <bean id="messageSource" 
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>
    </bean>
    
    <!-- 下面两个实现国际化的配置,可以实现中英文在页面上的跳转切换,
    只需在这儿配置和在i18n.jsp页面中添加两个超链接<br><br>
        <a href="i18n?locale=zh_CH">中文</a>
        <a href="i18n?locale=en_US">英文</a>
     -->
    <!-- 配置 SessionLocalResolver(实现国际化时使用) -->
    <bean id="localeResolver" 
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
    
    <!-- 配置LocaleChanceInterceptor(也是实现国际化的时候使用) -->
    <mvc:interceptors>
        <!-- 配置LocaleChanceInterceptor(也是实现国际化的时候使用) -->
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
        
    </mvc:interceptors>
    
    <!-- 使用这个标签可以不用 handler方法,直接跳转到某个页面页面
    (因为此xml文件中有视图解析器,所以,只需写需要跳转到的jsp页面的名字即可)
    <mvc:view-controller path="/i18n" view-name="i18n"/>-->
    
    <mvc:view-controller path="/i18n2" view-name="i18n2"/>
</beans>

 

实现国际化的file文件:这儿有三个,开发时可以写多个;

1)i18n.properties

i18n.user=User
i18n.password=Password

2)i18n_zh_CN.properties,中文的user和password

i18n.user=\u7528\u6237\u540D
i18n.password=\u5BC6\u7801

3)i18n_en_US.properties ,英文的user和password

i18n.user=User
i18n.password=Password

 

是国际化的handler类方法:这儿可以不使用该方法,直接在springmvc.xml文件中进行配置就行,为了验证,所以使用了此方法,输出国际化的user值

package com.atguigu.springmvc.crud.test;

@Controller
public class SpringMVCTest {
    
    @Autowired
    private EmployeeDao employeeDao;
    
    //实现国际化的类;
    @Autowired
    private ResourceBundleMessageSource messageSource;
    
    //实现国际化的方法,可以打印user
    @RequestMapping("/i18n")
    public String testI18n(Locale locale){
        String val=messageSource.getMessage("i18n.user", null, locale);
        System.out.println(val);
        return "i18n";
    }
}

 

三个jsp页面

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>
    
    <center>
  
    <!--  
        关于国际化:
        1. 在页面上能够根据浏览器语言设置的情况对文本(不是内容), 时间, 数值进行本地化处理
        2. 可以在 bean 中获取国际化资源文件 Locale 对应的消息
        3. 可以通过超链接切换 Locale, 而不再依赖于浏览器的语言设置情况
        
        解决:
        1. 使用 JSTL 的 fmt 标签
        2. 在 bean 中注入 ResourceBundleMessageSource 的示例, 使用其对应的 getMessage 方法即可
        (即在springmvc.xml文件中配置),
        3. 配置 LocalResolver 和 LocaleChangeInterceptor
        4.建立。。。.properties的配置文件
        国际化实现的步骤:
        需要有file文件,需要在springmvc.xml文件中配置,需要在jsp页面中使用jstl标准标签库,
        亦可以在handler方法中获取.properties文件中的值
    -->    
    <br><br>
    <a href="i18n">To i18n page</a>
    
    </center>
    
</body>
</html>

i18n.jsp:需要导入jstl标准标签库,使用fmt这个标签(国际化  标签),鼠标点击中文/英文,可以试问user和password值的中英文切换

<%@ 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>
    
    <fmt:message key="i18n.user"></fmt:message>
    
    
    <br><br>
    <a href="i18n2">To i18n2 page</a>
    
    <br><br>
    <a href="i18n?locale=zh_CH">中文</a>
    
    <br><br>
    <a href="i18n?locale=en_US">英文</a>
    
</body>
</html>

i18n2.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>
    
    <fmt:message key="i18n.password"></fmt:message>
    
    <br><br>
    <a href="i18n">To i18n page</a>
    
</body>
</html>

 

posted @ 2016-10-08 15:36  G-&-D  阅读(2273)  评论(0编辑  收藏  举报