當前位置:才華齋>IT認證>JAVA認證>

關於Struts中文問題的解決方法

JAVA認證 閱讀(1.97W)

碰到struts中文問題時,在網上查了很多資料,想必碰到過此類問題的朋友也都查過,也都看到過差不多是同一篇文章

關於Struts中文問題的解決方法

但是依法炮製了若干遍,JSP頁面上仍然顯示的是亂碼,無奈,實踐出真知,只好自己一遍一遍的試驗,終於成功了,在windows的weblogic8下,和unix的`weblogic8下均正確顯示漢字。

以下是程式碼內容:

首先是JSP頁面的內容,最簡化的一個form

TestItem English :

TestItem Chinese :

注意,頁面的字符集一定要定義成gb2312,否則顯示不了正確的漢字了,程式碼上半部的logic:iterate 標籤是 action 處理完畢後,返回給頁面顯示的。至於form提交的部分,由struts機制去做了,我只把 testitem_config 這個 action 的原始碼給出,大家就看明白了:

public class TestItemConfigAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)throws Exception {

TestItemConfigForm pcForm = (TestItemConfigForm)form;

String[] entryIndexArray = ntrypro();

String testPartKey;

ArrayList testPartOptionsEn = new ArrayList();

ArrayList testPartOptionsCn = new ArrayList();

ServletContext context = getServlet()。getServletContext();

String file = ealPath(“/WEB-INF/classes/resource/erties”);

PropertiesFileOperate pOperate = new PropertiesFileOperate(file);

Properties property = roperties();

int testpartnum = eInt(roperty(“”));

if(perateFlag() != null && !“”。equals(perateFlag())) {

if(eInt(perateFlag()) == 1 &&

estItemEn() != null && !“”。equals(estItemEn())){

String addKeyEn = “.” + (testpartnum + 1);

String addKeyCn = “.” + (testpartnum + 1);

String addValueEn = estItemEn()。trim();

String addValueCn = estItemCn()。trim();

String addValueCnWirite = new String(ytes(“ISO-8859-1”));

fyProperties(“”, (testpartnum + 1) + “”);

roperties(addKeyEn, addValueEn);

roperties(addKeyCn, addValueCnWirite);

File();

pOperate = null;

}

if(eInt(perateFlag()) == 2 &&

estItemEn() != null && !“”。equals(estItemEn())){

int entryIndex = eInt(estItemIndex()。trim());

String addKeyEn = “.” + entryIndex;

String addKeyCn = “.” + entryIndex;

String addValueEn = estItemEn()。trim();

String addValueCn = estItemCn()。trim();

String addValueCnWirite = new String(ytes(“ISO-8859-1”));

fyProperties(addKeyEn, addValueEn);

fyProperties(addKeyCn, addValueCnWirite);

File();

pOperate = null;

}

if(eInt(perateFlag()) == 3){

for(int i = 0; i 《 th; i++){

String indexEntry = (entryIndexArray[i]。substring(1, entryIndexArray[i]。indexOf(“,”)))。trim();

String addKeyEn = “.” + indexEntry;

String addKeyCn = “.” + indexEntry;

fyProperties(addKeyEn, “”);

fyProperties(addKeyCn, “”);

}

File();

pOperate = null;

}

}

PropertiesFileOperate pOperateShow = new PropertiesFileOperate(file);

Properties propertyShow = roperties();

int testpartNumber = eInt(roperty(“”));

ArrayList array = new ArrayList();

for(int i = 1; i 《= testpartNumber; i++){

ArrayList arr = new ArrayList();

testPartKey = “.”+i;

if (roperty(testPartKey) != null &&

!“”。equals(roperty(testPartKey))){

(i+“”);

(roperty(testPartKey));

(roperty(testPartKey));

testPartKey = “.”+i;

(new String(roperty(testPartKey)。getBytes(),“gb2312”));

(roperty(testPartKey));

(arr);

}

}

ttribute(“box”,array);

t(mapping, request);

return Forward(“testitemone”);

}

}

這個 action 並不複雜, 首先它定義了一個 properties 檔案 erties,在web服務器下的/WEB-INF/classes/resource/下面,用來記錄頁面上輸入的內容,由於 String addValueCnWirite = new String(ytes(“ISO-8859-1”)) 這個語句進行了字元轉換,所以 properties 檔案中記錄的內容大概都是這樣子的:

.29=u7F1Du9699u5F02u5E38

如果把程式改成記錄到資料庫中,也應該是這個樣子,屬於Unicode編碼吧。

而當要把記錄的內容輸出到客戶端時候,new String(roperty(testPartKey)。getBytes(),“gb2312”)) 這個語句又把Unicode編碼轉換成了GB2312,所以要求JSP頁面charset=gb2312,呵呵,這樣在windows 和 unix兩個系統下都可以正常顯示中文了,絕對沒有問題。