SSM框架如何實現國際化

作者的個人分享網:分享時刻【www.itison.cn】

SSM框架如何實現國際化,這是非常實用

國際化:是程序在不作任何修改的情況下,可以在不同國家或地區和不同語言環境下,按照當地的語言格式習慣顯示字符

如何實現國際化:
1. 將程序中的提示信息、錯誤信息等放在資源文件中,爲不同國家/語言編寫對應的資源文件。
2. 資源文件由很多key和value組成,key保持不變,value根據國家/語言設定。
3. 這些資源文件使用共同的基名,通過在基名後面添加語言代碼、國家和地區代碼來進行區分****重點內容

現在我們來在SSM框架中實現國際化:
首先,在spring.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:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:p="http://www.springframework.org/schema/p"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/context 
	 http://www.springframework.org/schema/context/spring-context.xsd	
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
	 
	  
	  <!-- 配置數據源 -->
	  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql:///user" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
		<!-- 初始化連接大小 -->
		<property name="initialSize" value="0"></property>
		<!-- 連接池最大數量 -->
		<property name="maxActive" value="20"></property>
		<!-- 連接池最大空閒 -->
		<property name="maxIdle" value="20"></property>
		<!-- 連接池最小空閒 -->
		<property name="minIdle" value="1"></property>
		<!-- 獲取連接最大等待時間 -->
		<property name="maxWait" value="60000"></property>
	</bean>
	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自動掃描映射文件 -->
		<property name="mapperLocations" value="classpath:com/user/dao/*.xml"></property>
	</bean>
	
	<!-- DAO接口所在包名,Spring會自動查找其下的類 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.user.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
	<!-- 配置事務管理器  -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>

	<!-- 配置Spring國際化信息  -->
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="messages/message"></property>
	</bean>
	<bean id="CookieLocaleResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
		<property name="cookieMaxAge" value="604800"></property>
		<property name="defaultLocale" value="zh_CN"></property>
		<property name="cookieName" value="language"></property>
	</bean>
	
	
</beans>

按照需求創建對應的兩個資源文件,這裏創建的是中文和英文兩個資源文件,將它們放在新建文件夾messages下。
英文資源文件message_en.properties如下:

hi=hello
something=The People's Republic of China
Chinese=Chinese
English=English
index=Index
welcome=Welcome
otherPage=other Page
userName=userName
password=password
login=login
register=register
title=title

中文資源文件message_zh_CN.properties如下(中文資源文件中的中文會自動轉碼):

hi=\u4F60\u597D
something=\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FD
Chinese=\u6C49\u8BED
English=\u82F1\u8BED
otherPage=\u53E6\u4E00\u9875
index=\u9996\u9875
welcome=\u6B22\u8FCE
userName=\u7528\u6237\u540D
password=\u5BC6\u7801
login=\u767B\u9646
register=\u6CE8\u518C
title=\u7528\u6237\u767B\u9646

springMVC.xml配置文件如下(這個是SSM框架整合方式之一需要的配置,與國際化無關):

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		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-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	
	<!--  配置用註解的方式實現處理器 -->
	<context:component-scan base-package="com.user.*"></context:component-scan>
	
	
	<!-- 配置視圖解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
	 
	
</beans>

jsp頁面的編寫,要記得引入spring的標籤庫:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Spring國際化測試</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <form action="">
    	<table border="0" cellspacing="0">
    		<tr><th colspan="2"><spring:message code="title"></spring:message></th></tr>
    		<tr>
    			<td><spring:message code="userName"></spring:message></td>
    			<td><input type="text" name="uname"></td>
    		</tr>
    		<tr>
    			<td><spring:message code="password"></spring:message></td>
    			<td><input type="text" name="pwd"></td>
    		</tr>
    		<tr>
    			<td></td>
    			<td>
    				<input type="submit" value="<spring:message code="login"></spring:message>">
    				<input type="button" value="<spring:message code="register"></spring:message>">
    			</td>
    		</tr>
    	
    	</table>
    </form>
  </body>
</html>

項目目錄結構如下:

這裏寫圖片描述

接下來測試國際化,如果你的瀏覽器語言設置的是英文,這裏已一個簡單的登陸界面做測試:

這裏寫圖片描述

發佈了43 篇原創文章 · 獲贊 86 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章