关于生成静态页的思考

import java.io.BufferedReader; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 

/** 
* 一段将网页静态化的代码。 
* 
* @author 老紫竹 JAVA世纪网(java2000.net) 
* 
*/ 
public class Test { 
  /** 
   * 将信息转化为静态html 
   * 
   * @param sUrl 
   *          动态信息访问URL 
   * @param charset 
   *          编码类型 
   * @param sSavePath 
   *          存储为静态文件的目录 
   * @param sHtmlFile 
   *          生成的静态文件名,可以按信息的唯一ID+.html命名 
   * @throws IOException 
   */ 
  public static void convert2Html(String sUrl, String charset, 
      String sSavePath, String sHtmlFile) throws IOException { 

    int HttpResult; 
    URL url = new URL(sUrl); 
    URLConnection urlconn = url.openConnection(); 
    // 抽象类 URLConnection 
    // 是所有类的超类,它代表应用程序和 URL 
    // 之间的通信链接,通过在 URL 上调用 
    // openConnection 方法创建连接对象 
    urlconn.connect(); // 使用 connect 方法建立到远程对象的实际连接 
    HttpURLConnection httpconn = (HttpURLConnection) urlconn; 
    // 每个 
    // HttpURLConnection 
    // 实例都可用于生成单个请求, 
    // 但是其他实例可以透明地共享连接到 
    // HTTP 服务器的基础网络 
    HttpResult = httpconn.getResponseCode(); 
    // getResponseCode可以从 HTTP 
    // 响应消息获取状态码 
    if (HttpResult != HttpURLConnection.HTTP_OK) { 
    } else { 
      InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(), 
          charset); 
      BufferedReader in = new BufferedReader(isr); 
      String inputLine; 
      if (!sSavePath.endsWith("/")) { 
        sSavePath += "/"; 
      } 
      FileOutputStream fout = new FileOutputStream(sSavePath + sHtmlFile); 
      while ((inputLine = in.readLine()) != null) { 
        System.out.println(inputLine); 
        fout.write((inputLine+"\n").getBytes()); 
      } 
      in.close(); 
      fout.close(); 
    } 
  } 

  public static void main(String[] args) throws IOException { 
    Test ru = new Test(); 
    String filePath = ru.getClass().getResource(".").getPath().toString(); // 取得项目根目录 

    convert2Html("http://www.java2000.net/", "UTF-8", filePath + "/", 
        "aaaa.htm"); 
  } 
} 

 以上转自老子竹,(上面是生成静态页文件的方法,我要实现第一次访问时如果有该action对应的html静态文件那么直接访问,第一次访问如果没有那么对该url生成静态页文件并访问)
能不能通过struts2拦截器对有必要的请求就行拦截调用该段代码生成静态页文件。
1、当我访问某某action时首先获得request
2、通过request获得相应的请求的url
3、将url改写成某某.html如我访问的是 http://localhost:8080/inkaNetMis/demo.action 改写后的url为
http://localhost:8080/inkaNetMis/demo.[b]html[/b],
4、判定改写后url的HttpURLConnection.HTTP_OK状态 是否OK。 如果ok说明之前生成过url直接访问 如果not ok 那么调用该方法生成生成静态页并访问。

具体没有实现,希望有人能帮忙实现下;
具体问题:我怎么能在拦截器中做跳转呢?(通过resultType指定url还有别的办法吗?)
用response 或是 request的跳转还会被拦截器拦截 造成死循环。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值