1:在MyEclipse下面創(chuàng)建一個(gè)test的Web? Project,然后添加Spring相關(guān)的文件,在src根目錄下創(chuàng)建applicationContext.xml文件。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
?
?????? <bean id="messageSource" ?class="org.springframework.context.support.ResourceBundleMessageSource">
?
???? ?<property name="basename" value="messages"/>
??????</bean>
?
???????<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
???
</beans>
2:在src根目錄下面創(chuàng)建4個(gè)資源文件:分別是
messages_zh.properties
main.title=你好
messages_en.properties
main.title=Hello World!
messages_ja.properties
main.title=こんにちは
messages_ko.properties
main.title=??????
3:在WebRoot根目錄下面創(chuàng)建test.jsp
test.jsp
<%@ page language="java"? pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="WEB-INF/lib/spring.tld"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <title>Spring國(guó)際化</title>
??</head>
? <body>
?
??? <spring:message code="main.title" /><br>
??? <input type="button" value="<spring:message code="main.title" />"/><br>
??</body>
</html>
4:修改WEB-INF下面的web.xml
在web.xml加入
<context-param>
??<param-name>contextConfigLocation</param-name>
??<param-value>
???classpath*:/applicationContext*,classpath*:META-INF/applicationContext*.xml
??</param-value>
?</context-param>
?<listener>
??<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
?</listener>
這樣用Spring國(guó)際化的Test.jsp頁面就做好了:),此種方法是自動(dòng)默認(rèn)當(dāng)前用戶的語言,比如客戶端是日語系統(tǒng),就自動(dòng)尋找messages_ja.properties資源文件,是英語系統(tǒng),就自動(dòng)尋找messages_en.properties資源文件。
注意事項(xiàng):
1:用hibernate3.0,連接Mysql5.0數(shù)據(jù)庫。
?如果用hibernate.properties配置文件
?hibernate.connection.url jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
如果用hibernate.cfg.xml配置文件
jdbc:mysql://localhost:3306/test?useUnicode=true&useUnicode=true&characterEncoding=utf-8&mysqlEncoding=utf8
2:頁面的編碼方式,應(yīng)該選用utf-8
<%@ page language="java"? pageEncoding="UTF-8"%>
3:創(chuàng)建的數(shù)據(jù)庫的編碼方式也應(yīng)該選用utf-8,以及表,字段的編碼方式都應(yīng)選用utf-8
注意以上3點(diǎn)就可以解決國(guó)際化時(shí),所出現(xiàn)的頁面顯示亂碼問題,以及插入韓語時(shí),出現(xiàn)的data too long for column問題.
posted on 2006-12-30 11:22
fish的Blog 閱讀(5320)
評(píng)論(2) 編輯 收藏 所屬分類:
spring