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>
项目目录结构如下:
接下来测试国际化,如果你的浏览器语言设置的是英文,这里已一个简单的登陆界面做测试:
感谢您的阅读,欢迎参观我的个人网站:小嗨词典【 https://www.happydict.cn】