Topic: 求助:struts显示乱码问题

  Print this page

1.求助:struts显示乱码问题 Copy to clipboard
Posted by: forget_ai
Posted on: 2006-03-29 09:10

我用struts做了一个根据提交的信息自动生成静态页面的WEB应用,但是有两 个问题一直无法解决
1:路径问题,在eclipse下面启动tomcat我得到的实际路径是工程目录,运行时路径是tomcat的bin目录,关闭eclipse在外面启动tomcat得到的运行路径是tomcat主目录?为什么?
2:乱码问题,我试了好几种方法,但英文和数字能够正常显示,中文显示为乱码?怎么回事啊?我该怎么解决?

谢谢高手指导!!
我的工作环境是eclipse3.11+tomcat+myeclipse
下面是附件
这是eclipse工程文件:
(只能上传1M我把lib目录下面的文件另外传啊)

createhtmeclipse.rar (16.75k)

2.Re:求助:struts显示乱码问题 [Re: forget_ai] Copy to clipboard
Posted by: forget_ai
Posted on: 2006-03-29 09:14

这是里面生成页面的程序
对了上面的程序要把eclipse去掉就可以直接导入了

//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl

package com.romance.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.romance.struts.form.LogonForm;

/**
* MyEclipse Struts Creation date: 03-22-2006
*
* XDoclet definition:
*
* @struts.action path="/logon" name="logonForm" input="/index.jsp"
* scope="request" validate="true"
*/
public class LogonAction extends Action {

  // --------------------------------------------------------- Instance
  // Variables

  // --------------------------------------------------------- Methods

  /**
   * Method execute
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return ActionForward
   */
  private String path = new File("").getAbsolutePath();
  
  private String filePath = path + "\\template.html";// 源模板文件
  
  private String filename = String.valueOf(Calendar.getInstance()
      .getTimeInMillis())
      + ".html";// 根据时间取得文件名
  
  private String fileurl = path + "\\" + filename;// 生成的html文件保存路径
  
  private String templateContent;

  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    ActionMessages errors = new ActionMessages();
    LogonForm logonForm = (LogonForm) form;

    String titlehtm = logonForm.getTitle();
    String editerhtm = logonForm.getEditer();
    String contexthtm = logonForm.getContext();

    if (!titlehtm.equals("") && !editerhtm.equals("")
        && !contexthtm.equals("")) {

      // 读取模块文件
      FileInputStream fileinputstream = new FileInputStream(filePath);
      byte[] bytes = new byte[fileinputstream.available()];
      fileinputstream.read(bytes);
      fileinputstream.close();

      templateContent = new String(bytes);

      templateContent = templateContent.replaceAll("###title###",
          titlehtm); // 替换掉模块中相应的地方
      templateContent = templateContent.replaceAll("###editer###",
          editerhtm);
      templateContent = templateContent.replaceAll("###context###",
          contexthtm);
      templateContent = templateContent.replaceAll("###filename###",
          filename);

      // 建立文件输出流
      FileOutputStream fileoutputstream = new FileOutputStream(fileurl);
      byte tag_bytes[] = templateContent.getBytes();
      fileoutputstream.write(tag_bytes);
      fileoutputstream.close();

      return mapping.findForward("successed");

    } else {
      errors.add("请补全内容", new ActionMessage("error.msg"));
      saveErrors(request, errors);
      return (new ActionForward(mapping.getInput()));
    }

  }

}

3.Re:求助:struts显示乱码问题 [Re: forget_ai] Copy to clipboard
Posted by: hoden_w
Posted on: 2006-03-30 17:55

TO:楼主:
给一个得到当前路经的方法:
public String getClassPath() {
String strClassName = getClass().getName();
String strPackageName = "";
if (getClass().getPackage() != null) {
strPackageName = getClass().getPackage().getName();
}
String strClassFileName = "";
if (!"".equals(strPackageName)) {
strClassFileName = strClassName.substring(
strPackageName.length() + 1, strClassName.length());
} else {
strClassFileName = strClassName;
}
System.out.println(strPackageName);
URL url = null;
url = getClass().getResource(strClassFileName + ".class");
String strURL = url.toString();
strURL = strURL.substring(strURL.indexOf('/') + 1, strURL
.lastIndexOf('/'));
return strURL;
}

4.Re:求助:struts显示乱码问题 [Re: forget_ai] Copy to clipboard
Posted by: andilyliao
Posted on: 2006-04-04 08:58

eclipse要在配置时设置为UTF_8就可以解决此问题


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923