java 中文亂碼處理。
參考
http://china.eceel.com/article/study_for_character_encoding_java.htm
http://upurban.com/bbs/viewtopic.php?t=246
1。什么是utf-8,什么是ISO-8859-1,什么是GB2312,還有什么是unicode
2。java 程序的字符的表示格式
3。jsp 程序中文顯示處理實例
3。1
<%@ page pageEncoding="ISO-8859-1"%>和<%@ page pageEncoding="GB2312"%>和<%@ page
pageEncoding="UTF-8"%>各自的意思是什么,他們是否只對post提交有效!
request.setCharacterEncoding("UTF-8")是什么意思?有什么區(qū)別?還有
response.setCharacterEncoding("UTF-8"),優(yōu)先于下邊
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
setCharacterEncoding()該函數(shù)用來設(shè)置http請求或者相應(yīng)的編碼。
對于request,是指提交內(nèi)容的編碼,指定后可以通過getParameter()則直接獲得正確的字符串,如果不
指定,則默認使用iso8859-1編碼,需要進一步處理。參見下述"表單輸入"。值得注意的是在執(zhí)行
setCharacterEncoding()之前,不能執(zhí)行任何getParameter()。java doc上說明:This method must be
called prior to reading request parameters or reading input using getReader()。而且,該指定
只對POST方法有效,對GET方法無效。分析原因,應(yīng)該是在執(zhí)行第一個getParameter()的時候,java將會
按照編碼分析所有的提交內(nèi)容,而后續(xù)的getParameter()不再進行分析,所以setCharacterEncoding()無
效。而對于GET方法提交表單是,提交的內(nèi)容在URL中,一開始就已經(jīng)按照編碼分析所有的提交內(nèi)容,
setCharacterEncoding()自然就無效。
對于response,則是指定輸出內(nèi)容的編碼,同時,該設(shè)置會傳遞給瀏覽器,告訴瀏覽器輸出內(nèi)容所采用的
編碼。
3.2. jsp輸出
指定文件輸出到browser是使用的編碼,該設(shè)置也應(yīng)該置于文件的開頭。例如:<%@ page
contentType="text/html; charset= GBK" %>。該設(shè)置和response.setCharacterEncoding("GBK")等效。
4。java EE程序利用過濾器 處理中文問實例
提交數(shù)據(jù)的編碼格式
tomcat默認提交格式是ISO-8859-1
可以通過設(shè)置過濾器(只針對post提交)或修改server.xml 的URIencoding 編碼格式(只針對get提交)
達到你想要的 數(shù)據(jù)提交編碼格式。
總結(jié)
---by mylu 18:26 2007-5-20
posted @
2007-05-20 22:36 Super·shen BLOG 閱讀(1528) |
評論 (0) |
編輯 收藏
ORM
Object Relation Mapping
對象 關(guān)系 映射
對象 指實體域?qū)ο?br />關(guān)系 關(guān)系數(shù)據(jù)
模型
概念模型(實體-屬性)
關(guān)系數(shù)據(jù)模型(關(guān)系數(shù)據(jù)庫)
域模型(對象)
軟件分層
v - 表述層
c /
??? /業(yè)務(wù)層
m- 持久層(hibernate 技術(shù)實現(xiàn))
??? \數(shù)據(jù)層
mvc 對應(yīng) 各層次
概念實體關(guān)系
1對1
1對多
多對多
表與表之間的關(guān)系 參照完整性
外鍵
多對多
多對一
域?qū)ο笾g的 關(guān)系
關(guān)聯(lián) (一對一 一對多 多對多)
依賴 (一個類需要訪問另外一個類)
聚集 (一個類的對象是另一個類的一部分, 人和手)
一般化 (繼承關(guān)系)
域?qū)ο?br />?實體域?qū)ο? (實體EJB,POJO)
過程域?qū)ο? (會話EJB,消息驅(qū)動EJB,POJO)
事件域?qū)ο? ()
在hibernate中 一般只關(guān)注 實體域?qū)ο?和 過程域?qū)ο?/p>
域?qū)ο蟮年P(guān)系
?域?qū)ο蟮年P(guān)聯(lián)關(guān)系 是有方向的
體現(xiàn)在類的編碼不一樣的
單向關(guān)聯(lián)
雙向關(guān)聯(lián)
?
域?qū)ο蟮某志没?br />把對象從內(nèi)存中 保存到持久化設(shè)備中去
ORM 與? ORM模式
ORM模式是一種持久化技術(shù),還有其他模式的持久化技術(shù)。如主動域模式(BMP),JDO模式,CMP模式。
域模型和數(shù)據(jù)模型的各個不匹配之處
1,繼承
2,多對多
3,雙向
4。粒度
盡量少連接查詢,很消耗時間的操作
?
創(chuàng)建持久化類
1。持久化類符合javabean的規(guī)范,包含一些屬性 以及對應(yīng)的getxxx 色天下學(xué)習(xí)方法
2。持久化類有一個id屬性,用來唯一表示類的每一個對象。 也叫OID 對象表示符
3。Hibernate要求持久化類必須提供一個不帶參數(shù)的默認構(gòu)造方法
創(chuàng)建數(shù)據(jù)庫schema
創(chuàng)建對象-關(guān)系映射文件
(一般在eslispe中先創(chuàng)建數(shù)據(jù)庫 然后再創(chuàng)建持久化類以及映射文件)
hibernate 映射類型
hibernate的初始化
static{
try{
//根據(jù)默認位置的hibernate配置文件創(chuàng)建 configuration實例
Configuration config = new Configuration();
config.addClass(Customer.class);
//創(chuàng)建SessionFactory 實例
sessionFactory = config.buildSessinoFactroy();
}catch(Exception e){e.printStackTrace();}
}
SessionFactory 接口
一個SessionFactory 實例是對應(yīng)一個數(shù)據(jù)源的,應(yīng)用從SessionFactory 獲取session實例對象
1線程安全的
2重量級的,不能隨意創(chuàng)建和銷毀她的實例。
Session 接口
1 Session接口是hibernate應(yīng)用最為廣泛的接口。
2 Session也被稱為持久化管理器,它提供和持久化相關(guān)的操作
3 Session有以下特點
?a 不是線程安全的 所以應(yīng)避免多線程共用一個Session實例
?b Session實例是輕量級的,所謂輕量級是指他的創(chuàng)建和銷毀不需要消耗太多的資源。意味著程序中可以經(jīng)常創(chuàng)建和銷毀Session實例,保證不多線程使用Session對象。
Session接口的常用方法:
save()
update()
delete()
load()
Session執(zhí)行事務(wù)流程
Session session = factory.openSession();
Transaction tx;
try{
tx = session.beginTranscation();
//執(zhí)行事務(wù)
...
//提交事務(wù)
tx.commit();
}
catche(Exception e)
{//如果出現(xiàn)異常,撤消事務(wù)
if(tx!=null)tx.rollback();
throw e;
}
finally{
session.close(); //不管事務(wù)是否成功,最后都要關(guān)閉session對象
}
}
?
?
?
?
posted @
2007-02-07 14:32 Super·shen BLOG 閱讀(417) |
評論 (0) |
編輯 收藏
eXtremeComponents FAQ(中文版)
本文檔允許在遵守以下兩條原則的條件下被使用和傳播: 1)不能憑借本文檔索取任何費用 2)以任何方式(印刷物或電子版)使用和傳播時本文檔時,必須包含本版權(quán)申明
eXtremeComponents FAQ(中文)
Q: 如何使用導(dǎo)出功能
A: 為了使用導(dǎo)出功能,只需要在web.xml文件中加入eXtremeComponents的導(dǎo)出過濾器的配置,內(nèi)容如下:
<filter>
<filter-name>eXtremeExport</filter-name>
<filter-class>org.extremecomponents.table.filter.ExportFilter</filter-class>
<init-param>
<param-name>responseHeadersSetBeforeDoFilter</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>eXtremeExport</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Q: 傳入中文參數(shù)亂碼,如下頁面:
<form id="form1" name="form1" method="post" action="應(yīng)用eXtremeTable的action或是結(jié)果頁面名">
<select name="selecttype" size="6">
<option value="第一個">第一個</option>
<option value="第二個">第二個</option>
<option value="第三個">第三個</option>
</select>
<input type="text" name="username" />
<input type="submit" name="Submit" value="提交" />
</form>
當(dāng)你提交時含有eXtremeTable的結(jié)果頁面會自動取得頁面上的表單參數(shù),那怕是經(jīng)過了action的mapping.findForward("forward"),在我的試用過程中到頁面上會出現(xiàn)傳遞過去的參數(shù),但出現(xiàn)了亂碼問題,使用查詢(filter)功能是的中文參數(shù)問題類似。
A:
-
確認服務(wù)器的參數(shù)是否設(shè)置了正確的編碼,如果使用Tomcat請確認Server.xml:
<Connector port="80" URIEncoding="UTF-8" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false"
redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" />
-
添加編碼過濾器到你的應(yīng)用工程:
/*
* Copyright 1999-2001,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
/**
* <p>Example filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user's session.</p>
*
* @author Craig McClanahan
* @version $Revision: 1.3 $ $Date: 2004/02/28 03:35:22 $
*/
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
-
在web.xml中添加編碼過濾器配置:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Q:關(guān)于導(dǎo)出時中文文件名為亂碼的問題
A: 這是個bug,建議使用英文文件名,主要原因還是編碼問題。我們現(xiàn)在正在想辦法解決。
Q:導(dǎo)出時文件內(nèi)容亂碼
A:首先請確認使用的是extremecomponents-1.0.1-M5-A4版以后的版本
- Excle: 導(dǎo)出為Excle的中文問題已經(jīng)修正,默認的情況下支持導(dǎo)出中文,用戶不需要任何改動
- PDF : 由于extremecomponents使用了FOP來生成PDF文件,F(xiàn)OP在導(dǎo)出中文內(nèi)容時會產(chǎn)生亂碼。具體的解決方案 大家可以參考最新eXtremeComponents包:支持 PDF中文導(dǎo)出
Q:當(dāng)變量名為"action",在IE下執(zhí)行產(chǎn)生javascript錯誤
A: 內(nèi)部使用了一些關(guān)鍵字,就目前我所知的為"action"、"submit"。建議大家命名時盡量避免,如果大家必須使用,則可以使用table標簽的autoIncludeParameters參數(shù)設(shè)置為"false":
autoIncludeParameters="false"
Q:怎么樣格式化輸出表單中的數(shù)據(jù)
A: 你可以設(shè)置列的cell:
- 日期格式化: cell = " date " format = " yyyy-MM-dd "
- 數(shù)字格式化: cell="currency" format="###,###,##0.00"
詳細信息請參考指南
Q:怎么樣加入鏈接
A: 你可以參考下例:
<ec:table
var="pres"
items="presidents"
action="${pageContext.request.contextPath}/compact.run"
imagePath="${pageContext.request.contextPath}/images/table/compact/*.gif"
view="compact"
title="Compact Toolbar View"
showTooltips="false"
>
<ec:exportPdf
fileName="output.pdf"
tooltip="Export PDF"
headerColor="black"
headerBackgroundColor="#b6c2da"
headerTitle="Presidents"
text="PDF"
/>
<ec:exportXls
fileName="output.xls"
tooltip="Export Excel"
text="XLS"
/>
<ec:row>
<ec:column property="fullName" title="Name">
<a >${pres.fullName}</a>
</ec:column>
<ec:column property="nickName"/>
<ec:column property="term"/>
<ec:column property="born" cell="date"/>
<ec:column property="died" cell="date"/>
<ec:column property="career"/>
</ec:row>
</ec:table>
Q: 我想使用行的高亮顯示如何設(shè)置
A: 你只需要設(shè)置行標簽的highlightRow屬性: highlightRow="true"。eXtremeComponents提供了很多接口允許用戶按照自己的習(xí)慣來進行定制,包括:CSS、CELL、View。相關(guān)信息請參考指南。
0
by lucky
posted @
2007-01-26 16:04 Super·shen BLOG 閱讀(282) |
評論 (0) |
編輯 收藏
》對象之間方法調(diào)用,通過傳遞消息
OOP使各個對象各司其職,分別負擔(dān)執(zhí)行一組相關(guān)的任務(wù),如果一個對象要依賴于一個不在其范圍內(nèi)的方法,它就需要訪問包含該方法的第二個對象,即第一個對象需要第二個對象執(zhí)行這個方法(或者叫方法調(diào)用) 利用OOP術(shù)語,叫做一個對象向另外一個對象發(fā)送消息。
》對象的生成: 對象是在執(zhí)行過程中由其所屬的類動態(tài)生成的。 一個類可以生成多個不同的對象。
》 消息與方法的概念
對象之間的傳遞通過消息傳遞完成
一個發(fā)送消息的對象 發(fā)送的消息包含3個方面的內(nèi)容
1,接受消息的對象
2,接受對象應(yīng)用的方法。
3,方法所需要的參數(shù)。
》面向?qū)ο笞兂傻幕咎卣?br />1 封裝性 Encapsulation 把數(shù)據(jù)和操作組織在類內(nèi)
2?繼承性 Inheritance 通過類的繼承關(guān)系
3多態(tài)性Polymophism(在運行的時候體現(xiàn)) ??A通過方法重裁 B通過方法重寫,子類覆蓋父類的方法(接口一個種特殊的類哦)
posted @
2007-01-24 17:47 Super·shen BLOG 閱讀(362) |
評論 (0) |
編輯 收藏
域?qū)ο笾g的4種關(guān)系
1。關(guān)聯(lián)
指類之間的引用關(guān)系,是實體域?qū)ο蟮淖钇毡榈年P(guān)系。
一對一、一對多、多對多關(guān)聯(lián)
如果類A和類B關(guān)聯(lián),那么被引用的B將被定義為A的屬性。
Customer? 與 Order 是一對多的關(guān)聯(lián)關(guān)系
Order 類 包含 Customer類的 屬性
Customer? 類 包含 集合 Order?
2。依賴
posted @
2006-12-30 16:30 Super·shen BLOG 閱讀(245) |
評論 (0) |
編輯 收藏
一個J2EE 工程中涉及的對象
- 數(shù)據(jù)傳輸對象DTO
- 業(yè)務(wù)對象BO (實體業(yè)務(wù)對象 過程業(yè)務(wù)對象 事件業(yè)務(wù)對象)
- 數(shù)據(jù)訪問對象DAO
?
概念
持久化框架、ORM框架、DAO設(shè)計模式
他們的關(guān)系是:ORM框架是一種持久化框架,DAO是用于實現(xiàn)持久化框架的一種設(shè)計模式。
posted @
2006-12-27 17:14 Super·shen BLOG 閱讀(417) |
評論 (0) |
編輯 收藏
?ActionErrors ? errors ? = ? null; ? ?
? errors ? = ? new ? ActionErrors(); ? ?
? errors.add("isExist", ? new ? ActionError("error.isExist"));???
// errors.add("isExist", ? new ? ActionError("error.isExist"));???等效于errors.add("isExist", ? new ? ActionMessage("error.isExist"));????
? saveErrors(request, ? errors); ?
? return ? (mapping.findForward("failure"));???
failure頁面里也定義了<html:errors?? name="isExist"/> ?
? ApplicationResources.properties里面也定義了error.isExist=user ? have ? already ? exist!!!???
? 運行結(jié)果 跳轉(zhuǎn)到failure頁面,顯示“user ? have ? already ? exist!!!???”
ActionErrors.GLOBAL_ERROR
怎么理解
它和我們使用普通的字符有什么區(qū)別啊
部分代碼如下:
err.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.logon"));
err.add("errinfo", new ActionError("error.logon"));
以上兩名有什么區(qū)別啊
沒什么區(qū)別
ActionErrors.GLOBAL_ERROR也是一個字符串。 最好寫做ActionErrors.GLOBAL_ERROR
不然的話可能會報錯。
posted @
2006-12-20 18:03 Super·shen BLOG 閱讀(2601) |
評論 (3) |
編輯 收藏
和女主角Action 對象共舞
什么是Action?
和常規(guī)的web 應(yīng)用相比,Struts Action 類工作起來就象一個小型的servlet。在大多數(shù)Java
應(yīng)用中,諸如訪問業(yè)務(wù)層的任務(wù)、錯誤處理等任務(wù)均是由servlet 承擔(dān)的。在一個 Struts 應(yīng)
用中,servlet 扮演著一個分派器的角色。而Action 對象則干實際的工作。象 servlets 一樣,
Action 對象是多線程的。每個應(yīng)用只需要一個Action 類的實例。
Action做些什么?一個典型的Action 的職責(zé)通常是:
?? ■校驗前提條件或者聲明
?? ■調(diào)用需要的業(yè)務(wù)邏輯方法
?? ■檢測其它處理錯誤
?? ■將控制路由到相關(guān)視圖檢驗輸入: Action 需要做的就是確認ActionForm 是否是需要的類型。
調(diào)用邏輯業(yè)務(wù):Action 類是HTTP 與應(yīng)用系統(tǒng)中其它部分之間的適配器。最重要的是要避免將業(yè)務(wù)邏輯放入
Action 之中。Action 類應(yīng)該只是簡單地收集業(yè)務(wù)方法需要的數(shù)據(jù)并傳遞它到具體的業(yè)務(wù)對
象。如果你同時在編寫業(yè)務(wù)類和Action 類,可能會受到要將它們編寫在一起的誘惑。一定
要抵擋這種誘惑,并且將業(yè)務(wù)方法放入Action 可調(diào)用的單獨的類之中。Java 虛擬機(JVM)
針對這種方法調(diào)用作了優(yōu)化;性能損失可以忽略不計。
同時你也得到了一些設(shè)計上的優(yōu)勢
Action檢測錯誤:
Struts具有一個設(shè)計良好的錯誤處理系統(tǒng),允許你可以:
??■ 同時截獲幾個錯誤
??■ 在請求中傳遞錯誤數(shù)據(jù)包
??■ 顯示本地化信息
這個處理流程涉及到兩個對象 (ActionErrors 和 ActionError) 和一個注冊錯誤的工
具方法(saveErrors) 。其它兩個對象 (MessageResources 和 一個定制標簽)則用來顯
示錯誤信息
注冊錯誤總體流程歸結(jié)為:
??1 創(chuàng)建一個空的ActionErrors 實例
??2 在錯誤發(fā)生時,為錯誤信息添加關(guān)鍵字;
??3 檢查是否添加了某些信息
??4 保存ActionErrors 集合對象到請求中
??5 ?轉(zhuǎn)發(fā)控制到錯誤頁面以顯示信息
??6? 否則,正常繼續(xù)
例如
ActionErrors?errors?=?new?ActionErrors();

try?
{
//?*?調(diào)用業(yè)務(wù)對象?*
}

catch?(ModelException?e)?
{
errors.add(ActionErrors.GLOBAL_ERROR,
new?ActionError("error.detail",e.getMessage()));
}

if?(!errors.empty())?
{
saveErrors(Request,?errors);
return?(mapping.findForward("error"));
}
//?*?正常繼續(xù)?*
posted @
2006-12-14 10:32 Super·shen BLOG 閱讀(427) |
評論 (0) |
編輯 收藏
用Struts開發(fā)Web應(yīng)用
要使用Struts 開發(fā)web 應(yīng)用,開發(fā)人員將需要的超鏈接定義為ActionForward,HTML 表
單定義為ActionForm,定制的服務(wù)器端動作定義為Action 類。
需要訪問JDBC 和EJB 的開發(fā)人員也可通過Action 對象進行他們的工作。這樣,表現(xiàn)層不
需要和Model 層打交道。Struts Action 對象將收集View 需要的數(shù)據(jù),然后將它們轉(zhuǎn)發(fā)到
表現(xiàn)頁面。Struts 提供 JSP 標記庫,它們將和JSP 頁面一起使用,簡化 HTML 表單和訪
問Action 要轉(zhuǎn)發(fā)的其它數(shù)據(jù)。其它表現(xiàn)機制,比如Velocity templates, 也可用來訪問
Struts 框架,以創(chuàng)建動態(tài)的web 頁面。這種處理流程入下圖:

posted @
2006-12-06 17:50 Super·shen BLOG 閱讀(293) |
評論 (0) |
編輯 收藏
?
Java本身就支持多國語言編碼,不需要寫任何程序,可以很簡單的
實現(xiàn)。
秘訣就是兩點:
1、所有HTML/JSP頁面全部采用UTF-8編碼
2、客戶端瀏覽器完全支持UTF-8編碼
步驟:
1、首先把所有的HTML/JSP的ContentType都設(shè)為UTF-8
2、然后對于JSP程序中的非ASCII碼提示信息都不應(yīng)該寫在程序里面,都應(yīng)該放在
application.properties里面統(tǒng)一管理。
3、對HTML用native2ascii工具統(tǒng)一做一次處理,把HTML中的非ASCII碼都轉(zhuǎn)換為Unicode編碼。
4、針對不同的語言,寫不同的application.properties,比如說簡體中文是
application_zh_CN.properties,繁體中文是application_zh_TW.properties這樣,然后對這些配置信
息文件同樣用native2ascii工具處理一次,把非ASCII碼統(tǒng)統(tǒng)轉(zhuǎn)為Unicode編碼。
5、在Servlet的request.getCharacterEncoding()獲得客戶端的操作系統(tǒng)默認編碼,然后set到Struts
的HTTPSession的Locale中。
OK!現(xiàn)在不同的客戶訪問,就會顯示不同的語言版本了。你可以看看此時你的瀏覽器的字符集,就是
UTF-8。現(xiàn)在你的網(wǎng)站和Google一樣了,嘿嘿,其實你有心的話,看看你的瀏覽器訪問Google的時候是
什么字符集吧
切記:所有的HTML/JSP都要設(shè)為UTF-8編碼,所有的文件中的非ASCII碼字符都要用native2ascii工具轉(zhuǎn)
為用ASCII表示的Unicode編碼。
----------------------------------------
----------------------------------------
原創(chuàng)
----------------------------------------
上面所述是我從網(wǎng)上下的一篇于中文問題的解決方案,確切的說應(yīng)該是關(guān)于Struts的國際化問題,下面我結(jié)合我的實踐談?wù)劸唧w如何實現(xiàn)Struts的國際化問題,我對理論不是非常精通,我只能完全憑自己的理解和實踐來講述,所以下面講的內(nèi)容可能不是非常正確,還請大家原諒。但有一點可以肯定,我通過自己的努力解決了Struts的中文問題,并實現(xiàn)Struts的國際化,其實一切并不復(fù)雜,下面是具體步驟:
0.遇到的問題(這些問題也許不會同時出現(xiàn))
a.中文數(shù)據(jù)從數(shù)據(jù)庫中到j(luò)sp中后就變成了"????"
b.做好的中文properties文件,其中的中文value在頁面顯示亂碼
c.jsp文件中的中文到瀏覽器后顯示時也是亂碼(建議不要在jsp文件中輸入中文,盡量放在properties文件中)
d.由jsp傳給bean的中文值,再由bean傳回頁面又是亂碼
e.當(dāng)更換本地瀏覽器的語言選項時,Web應(yīng)用程序不能自動根據(jù)你的locale選擇合適的*.properties文件。導(dǎo)致Web應(yīng)用程序不能國際化。
1.環(huán)境:
Web服務(wù)器: Tomcat 5.0.19
操作系統(tǒng): Win2000 Server
JVM : jdk 1.4
數(shù) 據(jù) 庫: Oracle 8.1.7
開發(fā)工具: struts studio 5.2 pro for eclipse
2.先將所有*.jsp 網(wǎng)頁中開頭處加入
<%@ page language="java" contentType="text/html; charset=utf-8" %>
再設(shè)置<html:html locale = "true">
3.然后編輯好兩個*.properties文件,放在classes文件夾下你指定的地方,這里是放在/web-inf/classes/com/wiley 下,它們分別是:
ApplicationResources.properties (英文資源文件)
ApplicationResources_zh.properties (中文資源文件)
隨便用什么工具編寫都行啊!
4.將ApplicationResources_zh.properties轉(zhuǎn)碼成gb2312。上面引文說要轉(zhuǎn)成UTF-8,結(jié)果我試了,不行。轉(zhuǎn)成gb2312就行了,操作是。
將ApplicationResources_zh.properties更名為ApplicationResources_xx.properties
在DOS命令行進入ApplicationResources_xx.properties所在的文件夾
使用命令:native2ascii -encoding gb2312 ApplicationResources_xx.properties ApplicationResources_zh.properties(至于你為什么會出現(xiàn)“native2ascii不是內(nèi)部命令”,,請查其它資料,可能你要設(shè)置環(huán)境變量,因為他是jdk的文件夾bin下的一個應(yīng)用程序)
5.接下來配置struts-config.xml,很簡單,我們加入:
<message-resources parameter="com.wiley.ApplicationResources"/> 就行了;
到此已能解決大多數(shù)中文問題。如上面所說的a,b,e 現(xiàn)在打開瀏覽器,選擇菜單:工具》internet選項》語言,將“中文-中國[zh-cn]”刪掉,添加一個“英語-英國[zh-gb]”確定后,重啟Tomcat,輸入網(wǎng)址你就會發(fā)現(xiàn),你的頁面的文本信息就會用的是ApplicationResources.properties (英文資源文件)中的內(nèi)容。如果換回“中文-中國[zh-cn]”,它就會顯示ApplicationResources_zh.properties (中文資源文件)中的中文內(nèi)容。
至于問題“c.jsp文件中的中文到瀏覽器后顯示時也是亂碼” 你就要用與第4步類似的方法來重新對*.jsp 文件編碼,這時-encoding的參數(shù)就要用UTF-8了,如果你用的也是struts studio 5.2 pro for eclipse工具,這一步就免了。它會自動用UTF-8的格式存儲。
至于問題“d.由jsp傳給bean的中文值,再由bean傳回頁面又是亂碼”的解決,我只是加了個過濾器。
你可以現(xiàn)在web.xml中加入:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.wiley.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
然后在你指定的包內(nèi)加個java文件 我放在了/web-inf/classes/com/wiley 里,下面是源代碼:
/*
* XP Forum
*
* Copyright (c) 2002-2003 RedSoft Group. All rights reserved.
*
*/
package com.huahang.tj.struts.filters;
import javax.servlet.*;
import java.io.IOException;
/**
* <p>Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user′s session.</p>
*
* @author <a href="mailto:jwtronics@yahoo.com">John Wong</a>
*
* @version $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $
*/
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}//EOC
到此我遇到的中文問題已全部得到解決,并從中理解到struts的國際化的深刻含義。
我個人覺得struts作為一個功能強大的應(yīng)用框架,應(yīng)該早就考慮到它的國際化問題,并在實際應(yīng)用中不會很復(fù)雜,只要我們遵循一些規(guī)則,就可以盡情享受struts給我們帶來的無窮樂趣。希望以上所述對大家有所幫助。
posted @
2006-12-06 16:46 Super·shen BLOG 閱讀(306) |
評論 (0) |
編輯 收藏