Java认证: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 = pcForm.getEntrypro();
String testPartKey;
ArrayList testPartOptionsEn = new ArrayList();
ArrayList testPartOptionsCn = new ArrayList();
ServletContext context = getServlet()。getServletContext();
String file = context.getRealPath(“/WEB-INF/classes/resource/TestItem.properties”);
PropertiesFileOperate pOperate = new PropertiesFileOperate(file);
Properties property = pOperate.getProperties();
int testpartnum = Integer.parseInt(property.getProperty(“test.item.num”));
if(pcForm.getOperateFlag()!= null && !“”。equals(pcForm.getOperateFlag())) {
if(Integer.parseInt(pcForm.getOperateFlag()) == 1 &&
pcForm.getTestItemEn()!= null && !“”。equals(pcForm.getTestItemEn())){
String addKeyEn = “test.item.en.” + (testpartnum + 1);
String addKeyCn = “test.item.cn.” + (testpartnum + 1);
String addValueEn = pcForm.getTestItemEn()。trim();
String addValueCn = pcForm.getTestItemCn()。trim();
String addValueCnWirite = new String(addValueCn.getBytes(“ISO-8859-1”));
pOperate.modifyProperties(“test.item.num”,(testpartnum + 1) + “”);
pOperate.addProperties(addKeyEn, addValueEn);
pOperate.addProperties(addKeyCn, addValueCnWirite);
pOperate.saveFile();
pOperate = null;
}
查看评论 回复