我想通过后台当我添加完,提交后就会在前台生成一个html的静态页面
比如我添加了一篇文章是XX,平时我们是通过wenzhang.do?action=view&id=xx
但是我要生成静态页面,让客户看到的是静态页面,提高访问速度等;
应该怎样做呢?
我到网上找了很多资料看,有些生成html是只能生成index的页面,后面要生成某个文件夹里面的就不能用了。。
请高手指教

解决方案 »

  1.   

    竹子写过一篇,用 httpClient来生成
      

  2.   

    给你一个类GetStaticPage,好像需要用到一两个包commons-httpclient-3.0.jar之类的,你自己找找
    import java.io.*;import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.GetMethod;import com.qingke.logs.Logs;public class GetStaticPage {
    String host;
    int port;

    public String getHost() {
    return host;
    } public void setHost(String host) {
    this.host = host;
    } public int getPort() {
    return port;
    } public void setPort(int port) {
    this.port = port;
    }

    public void jspToHtml(String jspPath,String jspName,String htmlPath,String htmlName){
       try{
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host,port);
           String allpath=jspPath+jspName;
           String allpathto = htmlPath+htmlName;
           GetMethod  get = new GetMethod(allpath);
           client.executeMethod(get);
           StringBuffer resultBuffer = new StringBuffer();
           BufferedReader in = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(),"GBK"));
           String inputLine = null;
           while( (inputLine = in.readLine()) != null ){
            resultBuffer.append(inputLine);
            resultBuffer.append("\n");
           }
           in.close();
           String s = resultBuffer.toString();
           //String s = get.getResponseBodyAsString();
           BufferedWriter bw = new BufferedWriter(new FileWriter(new File(allpathto)));
           bw.write(s);
           bw.close();
           get.releaseConnection();
         }catch (Exception ex ){
           Logs.writerLog(ex.getMessage());
        }
    } }在JSP中用的时候GetStaticPage gethtml = new GetStaticPage();
    String pathreal=request.getRealPath("");
    gethtml.setHost("127.0.0.1");
    gethtml.setPort(80);
    gethtml.jspToHtml("/程序路径/","wenzhang.do?action=view&id=xx",pathreal+"\\你的路径\\",文件名+".html");
      

  3.   

    回复3楼的,我要是在添加完后(就是插入数据库)后就自动生成html的页面,你那个好像不是吧
      

  4.   

    建议 看看 xsl应用 用xsl写个模板  然后后台内容提交后生成xml文件 调用模板转换成新的html
      

  5.   

    恩。。 简单点说
    假设 有一个HTML文件 你用JAVA字符流把他读进来的时候变成字符串 然后再将该字符串中的一部分替换掉 就会形成一个新的字符串 然后再将该字符串写到一个新的HTML文件里更概括的说:设计图与汽车的关系