這是很頭痛的問題,一直都是在困擾著我們。我有一個不算簡單也不算難的方法,這是我一直在用的方式。
1、建立一個類,包括一個格式將中文轉換為ISO8859-1編碼的方法:
publicclass Format2Chinese {
public Format2Chinese() {
}
public String format2IS08859(String str) {
try {
if (str == null || str.trim().equals(""))
str = "";
else
str = new String(str.getBytes("ISO8859-1"));
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
2、你的bean里面這時就要加一些東西了,如下面這個簡單的bean:
publicclass Leavemsg
{
public Leavemsg() {
}
//這個要加的,因為寫入的時候我們不格式,寫出的時候格式化
public Leavemsg(boolean format) {
this.format = format;
}
private String msg;
booleanformat = false; //用于確定是否將字符格式轉換
Format2Chinese function = new Format2Chinese();
publicvoid setMsg(String msg) {
if (format) {
this.msg = function.format2IS08859(msg);
} else
this.msg = msg;
}
public String getMsg() {
return msg;
}
}
3、在用bean裝數據的時候,這樣聲明:
Leavemsg msg=new Leavemsg(true); 然后其它的操作都是一樣的,就OK了。
這時頁面顯示中文的時候就不會出問題了,在JSP頁面里這可以這樣,用該類做包裝得到的內容。
posted on 2007-09-27 20:27
jadmin 閱讀(64)
評論(0) 編輯 收藏