碰到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兩個系統下都可以正常顯示中文了,絕對沒有問題。