Java SpringMVC實現(xiàn)國際化整合案例分析(i18n)
所謂國際化就是支持多種語言,web應用在不同的瀏覽環(huán)境中可以顯示出不同的語言,比如說漢語、英語等。下面我將以具體的實例來舉例說明:
(1)新建動態(tài)Java web項目,并導入幾個SpringMVC必需的幾個jar包,項目結構圖和所需jar包如下:

(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>
常規(guī)配置,沒有什么特殊的地方,不多解釋
(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>
<!-- 存儲區(qū)域設置信息 -->
<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類通過一個預定義會話名將區(qū)域化信息存儲在會話中。緊接著的“messageSource”配置的是國際化資源文件的路徑,”classpath:messages”指的是classpath路徑下的messages_zh_CN.properties文件和messages_en_US.properties文件。在這個配置文件的最后配置的是一個攔截器,該攔截器通過名為”lang”的參數(shù)來攔截HTTP請求,使其重新設置頁面的區(qū)域化信息
(4)兩個國際化資源文件:
i)messages_zh_CN.properties文件:
language.cn = \u4e2d\u6587 language.en = \u82f1\u6587 internationalisation = \u56fd\u9645\u5316 welcome = \u6b22\u8fce\u8bbf\u95ee\u201c\u007a\u0069\u0066\u0061\u006e\u0067\u0073\u006b\u0079\u7684\u4e2a\u4eba\u535a\u5ba2\u201d\uff0c\u0055\u0052\u004c\uff1a\u0068\u0074\u0074\u0070\u003a\u002f\u002f\u0077\u0077\u0077\u002e\u007a\u0069\u0066\u0061\u006e\u0067\u0073\u006b\u0079\u002e\u0063\u006e
ii)messages_en_US.properties文件:
language.cn = Chinese language.en = English internationalisation = \u0020Internationalisation welcome = Welcome to visit "zifangsky's personal blog",URL\uff1ahttp://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)英文:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot前后端json數(shù)據(jù)交互的全過程記錄
現(xiàn)在大多數(shù)互聯(lián)網(wǎng)項目都是采用前后端分離的方式開發(fā),下面這篇文章主要給大家介紹了關于SpringBoot前后端json數(shù)據(jù)交互的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-03-03
基于Jenkins自動打包并部署docker環(huán)境的操作過程
這篇文章主要介紹了基于Jenkins自動打包并部署docker環(huán)境,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
配置pom.xml用maven打包java工程的方法(推薦)
下面小編就為大家?guī)硪黄渲胮om.xml用maven打包java工程的方法(推薦)。小編覺得挺不錯的, 現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06

