中国教育在线 中国教育网 加入收藏 设为首页

Java认证:Struts中文问题解决方法

http://itpx.eol.cn  来源:  作者:网络转载  2011-09-14    

  碰到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;

  }

推荐给好友    我要收藏    我要纠错    分享到

免责声明:

① 凡本站注明“稿件来源:中国教育在线”的所有文字、图片和音视频稿件,版权均属本网所有,任何媒体、网站或个人未经本网协议授权不得转载、链接、转贴或以其他方式复制发表。已经本站协议授权的媒体、网站,在下载使用时必须注明“稿件来源:中国教育在线”,违者本站将依法追究责任。

② 本站注明稿件来源为其他媒体的文/图等稿件均为转载稿,本站转载出于非商业性的教育和科研之目的,并不意味着赞同其观点或证实其内容的真实性。如转载稿涉及版权等问题,请作者在两周内速来电或来函联系。

内容推荐
eol.cn简介 | 联系方式 | 网站声明 | 京ICP证140769号 | 京ICP备12045350号 | 京公网安备 11010802020236号
版权所有 北京中教双元科技集团有限公司 EOL Corporation
Mail to: webmaster@eol.cn