加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java SpringMVC实现国际化整合案例分析(i18n)

发布时间:2020-12-14 20:49:57 所属栏目:Java 来源:网络整理
导读:所谓国际化就是支持多种语言,web应用在不同的浏览环境中可以显示出不同的语言,比如说汉语、英语等。下面我将以具体的实例来举例说明: (1)新建动态Java web项目,并导入几个SpringMVC必需的几个jar包,项目结构图和所需jar包如下: (2)配置web.xml: w

所谓国际化就是支持多种语言,web应用在不同的浏览环境中可以显示出不同的语言,比如说汉语、英语等。下面我将以具体的实例来举例说明:

(1)新建动态Java web项目,并导入几个SpringMVC必需的几个jar包,项目结构图和所需jar包如下:

wKioL1cswV2Q15n3AAAe96yM-S0863.png


(2)配置web.xml:

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

常规配置,没有什么特殊的地方,不多解释

(3)SpringMVC的配置文件springmvc-servlet.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-4.0.xsd 
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-4.0.xsd
              http://www.springframework.org/schema/mvc 
              http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 
  <context:component-scan base-package="cn.zifangsky.* *.controller" />
 
  <context:annotation-config /> <!-- 激活Bean中定义的注解 -->
  <mvc:annotation-driven />
 
  <!-- 视图相关配置 -->
  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" /> <!-- 视图前缀 -->
    <property name="suffix" value=".jsp" /> <!-- 视图后缀 -->
  </bean>
  <!-- 存储区域设置信息 -->
  <bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
  <!-- 国际化资源文件 -->
  <bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
  </bean>
 
  <mvc:interceptors>
    <bean id="localeChangeInterceptor"
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
      <property name="paramName" value="lang" />
    </bean>
  </mvc:interceptors>
</beans>

在上面的配置中,SessionLocaleResolver类通过一个预定义会话名将区域化信息存储在会话中。紧接着的“messageSource”配置的是国际化资源文件的路径,”classpath:messages”指的是classpath路径下的messages_zh_CN.properties文件和messages_en_US.properties文件。在这个配置文件的最后配置的是一个拦截器,该拦截器通过名为”lang”的参数来拦截HTTP请求,使其重新设置页面的区域化信息

(4)两个国际化资源文件:

i)messages_zh_CN.properties文件:

language.cn = u4e2du6587
language.en = u82f1u6587
internationalisation = u56fdu9645u5316
welcome = u6b22u8fceu8bbfu95eeu201cu007au0069u0066u0061u006eu0067u0073u006bu0079u7684u4e2au4ebau535au5ba2u201duff0cu0055u0052u004cuff1au0068u0074u0074u0070u003au002fu002fu0077u0077u0077u002eu007au0069u0066u0061u006eu0067u0073u006bu0079u002eu0063u006e

ii)messages_en_US.properties文件:

language.cn = Chinese
language.en = English
internationalisation = u0020Internationalisation
welcome = Welcome to visit "zifangsky's personal blog",URLuff1ahttp://www.zifangsky.cn

注:上面一些看起来“乱码”的地方实际上是经过Unicode编码的

(5)后台处理请求的controller:

package cn.zifangsky.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class I18nController {
 
  @RequestMapping(value = "/hello")
  public ModelAndView welcome() {
    ModelAndView modelAndView = new ModelAndView("welcome");
 
    return modelAndView;
  }
 
}

这个controller很简单,就是转到一个视图页面welcome.jsp

(6)首页的index.jsp:

<% response.sendRedirect("hello.html"); %>

意思很简单,就是项目启动之后就请求htllo.html,也就是让controller中的welcome方法处理这个请求

(7)welcome.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>SpringMVC<spring:message code="internationalisation" /></title>
</head>
<body>
  Language: <a href="?lang=zh_CN" rel="external nofollow" ><spring:message code="language.cn" /></a> - <a href="?lang=en_US" rel="external nofollow" ><spring:message code="language.en" /></a>
  <h2>
    <spring:message code="welcome" />
  </h2>
  Locale: ${pageContext.response.locale }
</body>
</html>

可以看出,在需要使用国际化处理的地方都使用了spring的message标签,code属性对应资源文件中的“键”名称

(8)最后的显示效果如下:

i)中文:

ii)英文:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • 详解SpringMVC学习系列之国际化
  • 学习SpringMVC――国际化+上传+下载详解
  • 浅谈SpringMVC国际化支持

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读