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")是什么意思?有什么區別?還有
response.setCharacterEncoding("UTF-8"),優先于下邊
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
setCharacterEncoding()該函數用來設置http請求或者相應的編碼。
對于request,是指提交內容的編碼,指定后可以通過getParameter()則直接獲得正確的字符串,如果不
指定,則默認使用iso8859-1編碼,需要進一步處理。參見下述"表單輸入"。值得注意的是在執行
setCharacterEncoding()之前,不能執行任何getParameter()。java doc上說明:This method must be
called prior to reading request parameters or reading input using getReader()。而且,該指定
只對POST方法有效,對GET方法無效。分析原因,應該是在執行第一個getParameter()的時候,java將會
按照編碼分析所有的提交內容,而后續的getParameter()不再進行分析,所以setCharacterEncoding()無
效。而對于GET方法提交表單是,提交的內容在URL中,一開始就已經按照編碼分析所有的提交內容,
setCharacterEncoding()自然就無效。
對于response,則是指定輸出內容的編碼,同時,該設置會傳遞給瀏覽器,告訴瀏覽器輸出內容所采用的
編碼。
3.2. jsp輸出
指定文件輸出到browser是使用的編碼,該設置也應該置于文件的開頭。例如:<%@ page
contentType="text/html; charset= GBK" %>。該設置和response.setCharacterEncoding("GBK")等效。
4。java EE程序利用過濾器 處理中文問實例
提交數據的編碼格式
tomcat默認提交格式是ISO-8859-1
可以通過設置過濾器(只針對post提交)或修改server.xml 的URIencoding 編碼格式(只針對get提交)
達到你想要的 數據提交編碼格式。
總結
---by mylu 18:26 2007-5-20
posted @
2007-05-20 22:36 Super·shen BLOG 閱讀(1524) |
評論 (0) |
編輯 收藏
ORM
Object Relation Mapping
對象 關系 映射
對象 指實體域對象
關系 關系數據
模型
概念模型(實體-屬性)
關系數據模型(關系數據庫)
域模型(對象)
軟件分層
v - 表述層
c /
??? /業務層
m- 持久層(hibernate 技術實現)
??? \數據層
mvc 對應 各層次
概念實體關系
1對1
1對多
多對多
表與表之間的關系 參照完整性
外鍵
多對多
多對一
域對象之間的 關系
關聯 (一對一 一對多 多對多)
依賴 (一個類需要訪問另外一個類)
聚集 (一個類的對象是另一個類的一部分, 人和手)
一般化 (繼承關系)
域對象
?實體域對象? (實體EJB,POJO)
過程域對象? (會話EJB,消息驅動EJB,POJO)
事件域對象? ()
在hibernate中 一般只關注 實體域對象 和 過程域對象
域對象的關系
?域對象的關聯關系 是有方向的
體現在類的編碼不一樣的
單向關聯
雙向關聯
?
域對象的持久化
把對象從內存中 保存到持久化設備中去
ORM 與? ORM模式
ORM模式是一種持久化技術,還有其他模式的持久化技術。如主動域模式(BMP),JDO模式,CMP模式。
域模型和數據模型的各個不匹配之處
1,繼承
2,多對多
3,雙向
4。粒度
盡量少連接查詢,很消耗時間的操作
?
創建持久化類
1。持久化類符合javabean的規范,包含一些屬性 以及對應的getxxx 色天下學習方法
2。持久化類有一個id屬性,用來唯一表示類的每一個對象。 也叫OID 對象表示符
3。Hibernate要求持久化類必須提供一個不帶參數的默認構造方法
創建數據庫schema
創建對象-關系映射文件
(一般在eslispe中先創建數據庫 然后再創建持久化類以及映射文件)
hibernate 映射類型
hibernate的初始化
static{
try{
//根據默認位置的hibernate配置文件創建 configuration實例
Configuration config = new Configuration();
config.addClass(Customer.class);
//創建SessionFactory 實例
sessionFactory = config.buildSessinoFactroy();
}catch(Exception e){e.printStackTrace();}
}
SessionFactory 接口
一個SessionFactory 實例是對應一個數據源的,應用從SessionFactory 獲取session實例對象
1線程安全的
2重量級的,不能隨意創建和銷毀她的實例。
Session 接口
1 Session接口是hibernate應用最為廣泛的接口。
2 Session也被稱為持久化管理器,它提供和持久化相關的操作
3 Session有以下特點
?a 不是線程安全的 所以應避免多線程共用一個Session實例
?b Session實例是輕量級的,所謂輕量級是指他的創建和銷毀不需要消耗太多的資源。意味著程序中可以經常創建和銷毀Session實例,保證不多線程使用Session對象。
Session接口的常用方法:
save()
update()
delete()
load()
Session執行事務流程
Session session = factory.openSession();
Transaction tx;
try{
tx = session.beginTranscation();
//執行事務
...
//提交事務
tx.commit();
}
catche(Exception e)
{//如果出現異常,撤消事務
if(tx!=null)tx.rollback();
throw e;
}
finally{
session.close(); //不管事務是否成功,最后都要關閉session對象
}
}
?
?
?
?
posted @
2007-02-07 14:32 Super·shen BLOG 閱讀(412) |
評論 (0) |
編輯 收藏
eXtremeComponents FAQ(中文版)
本文檔允許在遵守以下兩條原則的條件下被使用和傳播: 1)不能憑借本文檔索取任何費用 2)以任何方式(印刷物或電子版)使用和傳播時本文檔時,必須包含本版權申明
eXtremeComponents FAQ(中文)
Q: 如何使用導出功能
A: 為了使用導出功能,只需要在web.xml文件中加入eXtremeComponents的導出過濾器的配置,內容如下:
<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: 傳入中文參數亂碼,如下頁面:
<form id="form1" name="form1" method="post" action="應用eXtremeTable的action或是結果頁面名">
<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>
當你提交時含有eXtremeTable的結果頁面會自動取得頁面上的表單參數,那怕是經過了action的mapping.findForward("forward"),在我的試用過程中到頁面上會出現傳遞過去的參數,但出現了亂碼問題,使用查詢(filter)功能是的中文參數問題類似。
A:
-
確認服務器的參數是否設置了正確的編碼,如果使用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" />
-
添加編碼過濾器到你的應用工程:
/*
* 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:關于導出時中文文件名為亂碼的問題
A: 這是個bug,建議使用英文文件名,主要原因還是編碼問題。我們現在正在想辦法解決。
Q:導出時文件內容亂碼
A:首先請確認使用的是extremecomponents-1.0.1-M5-A4版以后的版本
Q:當變量名為"action",在IE下執行產生javascript錯誤
A: 內部使用了一些關鍵字,就目前我所知的為"action"、"submit"。建議大家命名時盡量避免,如果大家必須使用,則可以使用table標簽的autoIncludeParameters參數設置為"false":
autoIncludeParameters="false"
Q:怎么樣格式化輸出表單中的數據
A: 你可以設置列的cell:
- 日期格式化: cell = " date " format = " yyyy-MM-dd "
- 數字格式化: 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: 我想使用行的高亮顯示如何設置
A: 你只需要設置行標簽的highlightRow屬性: highlightRow="true"。eXtremeComponents提供了很多接口允許用戶按照自己的習慣來進行定制,包括:CSS、CELL、View。相關信息請參考指南。
0
by lucky
posted @
2007-01-26 16:04 Super·shen BLOG 閱讀(280) |
評論 (0) |
編輯 收藏
》對象之間方法調用,通過傳遞消息
OOP使各個對象各司其職,分別負擔執行一組相關的任務,如果一個對象要依賴于一個不在其范圍內的方法,它就需要訪問包含該方法的第二個對象,即第一個對象需要第二個對象執行這個方法(或者叫方法調用) 利用OOP術語,叫做一個對象向另外一個對象發送消息。
》對象的生成: 對象是在執行過程中由其所屬的類動態生成的。 一個類可以生成多個不同的對象。
》 消息與方法的概念
對象之間的傳遞通過消息傳遞完成
一個發送消息的對象 發送的消息包含3個方面的內容
1,接受消息的對象
2,接受對象應用的方法。
3,方法所需要的參數。
》面向對象變成的基本特征
1 封裝性 Encapsulation 把數據和操作組織在類內
2?繼承性 Inheritance 通過類的繼承關系
3多態性Polymophism(在運行的時候體現) ??A通過方法重裁 B通過方法重寫,子類覆蓋父類的方法(接口一個種特殊的類哦)
posted @
2007-01-24 17:47 Super·shen BLOG 閱讀(358) |
評論 (0) |
編輯 收藏
域對象之間的4種關系
1。關聯
指類之間的引用關系,是實體域對象的最普遍的關系。
一對一、一對多、多對多關聯
如果類A和類B關聯,那么被引用的B將被定義為A的屬性。
Customer? 與 Order 是一對多的關聯關系
Order 類 包含 Customer類的 屬性
Customer? 類 包含 集合 Order?
2。依賴
posted @
2006-12-30 16:30 Super·shen BLOG 閱讀(243) |
評論 (0) |
編輯 收藏
一個J2EE 工程中涉及的對象
- 數據傳輸對象DTO
- 業務對象BO (實體業務對象 過程業務對象 事件業務對象)
- 數據訪問對象DAO
?
概念
持久化框架、ORM框架、DAO設計模式
他們的關系是:ORM框架是一種持久化框架,DAO是用于實現持久化框架的一種設計模式。
posted @
2006-12-27 17:14 Super·shen BLOG 閱讀(413) |
評論 (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!!!???
? 運行結果 跳轉到failure頁面,顯示“user ? have ? already ? exist!!!???”
ActionErrors.GLOBAL_ERROR
怎么理解
它和我們使用普通的字符有什么區別啊
部分代碼如下:
err.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.logon"));
err.add("errinfo", new ActionError("error.logon"));
以上兩名有什么區別啊
沒什么區別
ActionErrors.GLOBAL_ERROR也是一個字符串。 最好寫做ActionErrors.GLOBAL_ERROR
不然的話可能會報錯。
posted @
2006-12-20 18:03 Super·shen BLOG 閱讀(2596) |
評論 (3) |
編輯 收藏
和女主角Action 對象共舞
什么是Action?
和常規的web 應用相比,Struts Action 類工作起來就象一個小型的servlet。在大多數Java
應用中,諸如訪問業務層的任務、錯誤處理等任務均是由servlet 承擔的。在一個 Struts 應
用中,servlet 扮演著一個分派器的角色。而Action 對象則干實際的工作。象 servlets 一樣,
Action 對象是多線程的。每個應用只需要一個Action 類的實例。
Action做些什么?一個典型的Action 的職責通常是:
?? ■校驗前提條件或者聲明
?? ■調用需要的業務邏輯方法
?? ■檢測其它處理錯誤
?? ■將控制路由到相關視圖檢驗輸入: Action 需要做的就是確認ActionForm 是否是需要的類型。
調用邏輯業務:Action 類是HTTP 與應用系統中其它部分之間的適配器。最重要的是要避免將業務邏輯放入
Action 之中。Action 類應該只是簡單地收集業務方法需要的數據并傳遞它到具體的業務對
象。如果你同時在編寫業務類和Action 類,可能會受到要將它們編寫在一起的誘惑。一定
要抵擋這種誘惑,并且將業務方法放入Action 可調用的單獨的類之中。Java 虛擬機(JVM)
針對這種方法調用作了優化;性能損失可以忽略不計。
同時你也得到了一些設計上的優勢
Action檢測錯誤:
Struts具有一個設計良好的錯誤處理系統,允許你可以:
??■ 同時截獲幾個錯誤
??■ 在請求中傳遞錯誤數據包
??■ 顯示本地化信息
這個處理流程涉及到兩個對象 (ActionErrors 和 ActionError) 和一個注冊錯誤的工
具方法(saveErrors) 。其它兩個對象 (MessageResources 和 一個定制標簽)則用來顯
示錯誤信息
注冊錯誤總體流程歸結為:
??1 創建一個空的ActionErrors 實例
??2 在錯誤發生時,為錯誤信息添加關鍵字;
??3 檢查是否添加了某些信息
??4 保存ActionErrors 集合對象到請求中
??5 ?轉發控制到錯誤頁面以顯示信息
??6? 否則,正常繼續
例如
ActionErrors?errors?=?new?ActionErrors();

try?
{
//?*?調用業務對象?*
}

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

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

posted @
2006-12-06 17:50 Super·shen BLOG 閱讀(289) |
評論 (0) |
編輯 收藏
?
Java本身就支持多國語言編碼,不需要寫任何程序,可以很簡單的
實現。
秘訣就是兩點:
1、所有HTML/JSP頁面全部采用UTF-8編碼
2、客戶端瀏覽器完全支持UTF-8編碼
步驟:
1、首先把所有的HTML/JSP的ContentType都設為UTF-8
2、然后對于JSP程序中的非ASCII碼提示信息都不應該寫在程序里面,都應該放在
application.properties里面統一管理。
3、對HTML用native2ascii工具統一做一次處理,把HTML中的非ASCII碼都轉換為Unicode編碼。
4、針對不同的語言,寫不同的application.properties,比如說簡體中文是
application_zh_CN.properties,繁體中文是application_zh_TW.properties這樣,然后對這些配置信
息文件同樣用native2ascii工具處理一次,把非ASCII碼統統轉為Unicode編碼。
5、在Servlet的request.getCharacterEncoding()獲得客戶端的操作系統默認編碼,然后set到Struts
的HTTPSession的Locale中。
OK!現在不同的客戶訪問,就會顯示不同的語言版本了。你可以看看此時你的瀏覽器的字符集,就是
UTF-8?,F在你的網站和Google一樣了,嘿嘿,其實你有心的話,看看你的瀏覽器訪問Google的時候是
什么字符集吧
切記:所有的HTML/JSP都要設為UTF-8編碼,所有的文件中的非ASCII碼字符都要用native2ascii工具轉
為用ASCII表示的Unicode編碼。
----------------------------------------
----------------------------------------
原創
----------------------------------------
上面所述是我從網上下的一篇于中文問題的解決方案,確切的說應該是關于Struts的國際化問題,下面我結合我的實踐談談具體如何實現Struts的國際化問題,我對理論不是非常精通,我只能完全憑自己的理解和實踐來講述,所以下面講的內容可能不是非常正確,還請大家原諒。但有一點可以肯定,我通過自己的努力解決了Struts的中文問題,并實現Struts的國際化,其實一切并不復雜,下面是具體步驟:
0.遇到的問題(這些問題也許不會同時出現)
a.中文數據從數據庫中到jsp中后就變成了"????"
b.做好的中文properties文件,其中的中文value在頁面顯示亂碼
c.jsp文件中的中文到瀏覽器后顯示時也是亂碼(建議不要在jsp文件中輸入中文,盡量放在properties文件中)
d.由jsp傳給bean的中文值,再由bean傳回頁面又是亂碼
e.當更換本地瀏覽器的語言選項時,Web應用程序不能自動根據你的locale選擇合適的*.properties文件。導致Web應用程序不能國際化。
1.環境:
Web服務器: Tomcat 5.0.19
操作系統: Win2000 Server
JVM : jdk 1.4
數 據 庫: Oracle 8.1.7
開發工具: struts studio 5.2 pro for eclipse
2.先將所有*.jsp 網頁中開頭處加入
<%@ page language="java" contentType="text/html; charset=utf-8" %>
再設置<html:html locale = "true">
3.然后編輯好兩個*.properties文件,放在classes文件夾下你指定的地方,這里是放在/web-inf/classes/com/wiley 下,它們分別是:
ApplicationResources.properties (英文資源文件)
ApplicationResources_zh.properties (中文資源文件)
隨便用什么工具編寫都行??!
4.將ApplicationResources_zh.properties轉碼成gb2312。上面引文說要轉成UTF-8,結果我試了,不行。轉成gb2312就行了,操作是。
將ApplicationResources_zh.properties更名為ApplicationResources_xx.properties
在DOS命令行進入ApplicationResources_xx.properties所在的文件夾
使用命令:native2ascii -encoding gb2312 ApplicationResources_xx.properties ApplicationResources_zh.properties(至于你為什么會出現“native2ascii不是內部命令”,,請查其它資料,可能你要設置環境變量,因為他是jdk的文件夾bin下的一個應用程序)
5.接下來配置struts-config.xml,很簡單,我們加入:
<message-resources parameter="com.wiley.ApplicationResources"/> 就行了;
到此已能解決大多數中文問題。如上面所說的a,b,e 現在打開瀏覽器,選擇菜單:工具》internet選項》語言,將“中文-中國[zh-cn]”刪掉,添加一個“英語-英國[zh-gb]”確定后,重啟Tomcat,輸入網址你就會發現,你的頁面的文本信息就會用的是ApplicationResources.properties (英文資源文件)中的內容。如果換回“中文-中國[zh-cn]”,它就會顯示ApplicationResources_zh.properties (中文資源文件)中的中文內容。
至于問題“c.jsp文件中的中文到瀏覽器后顯示時也是亂碼” 你就要用與第4步類似的方法來重新對*.jsp 文件編碼,這時-encoding的參數就要用UTF-8了,如果你用的也是struts studio 5.2 pro for eclipse工具,這一步就免了。它會自動用UTF-8的格式存儲。
至于問題“d.由jsp傳給bean的中文值,再由bean傳回頁面又是亂碼”的解決,我只是加了個過濾器。
你可以現在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>
然后在你指定的包內加個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作為一個功能強大的應用框架,應該早就考慮到它的國際化問題,并在實際應用中不會很復雜,只要我們遵循一些規則,就可以盡情享受struts給我們帶來的無窮樂趣。希望以上所述對大家有所幫助。
posted @
2006-12-06 16:46 Super·shen BLOG 閱讀(300) |
評論 (0) |
編輯 收藏