通过过滤器生成静态页面-springmvc

8 篇文章 0 订阅
public class StateHtmlFilter extends OncePerRequestFilter {

    private boolean debug=true;
    String stres="";
    private FilterConfig filterConfig=null;
    public void StateHtmlFilter(FilterConfig filterConfig){
        this.filterConfig=filterConfig;
    }
 
    @Override
    protected void doFilterInternal(HttpServletRequest request,
            HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException {
         // 请求的uri  
        String uri = request.getRequestURI();  
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
     // uri中包含background时才进行过滤  
        
        HttpServletResponse httpRes=(HttpServletResponse) response;
        String path=((HttpServletRequest)request).getServletPath();
        ///stres="html"+path.substring(4);
        stres= path.substring(1);                //获取文件名
        String pString=request.getSession().getServletContext().getRealPath("/");//
    if (uri.indexOf("jsp") != -1) {
        ResponseWrapper responseWrapper=new ResponseWrapper(httpRes); //创建自定义的应答对象
        chain.doFilter(request,responseWrapper);                    //完成过滤连的业务
        responseWrapper.getWriter().flush();
        int len=stres.length();                                        //获取文件名的长度
        String strs=stres.substring(0,len-4)+".html";                //定义静态文件的名称
        String jspPath=request.getRealPath(stres);                    //获取文件的真实存储路径
        File  jspFile=new File(jspPath);                            //创建index.jsp文件对象
        
        //String static_path=jspFile.getParent() ;
        File htmlFile=new File(pString+"/html/",strs.substring(4));                        //定义静态文件的存储路径
        Date htmlDate=null;
        Date now=new Date();
        if (htmlFile.exists()) {
            htmlDate=new Date(htmlFile.lastModified());
        }else{
            htmlFile.createNewFile();
        }
        if(debug || htmlDate==null || htmlDate.getDate()!=now.getDate()){
            FileOutputStream fileStream=new FileOutputStream(htmlFile);//创建HTML文件的输出流
            DataOutputStream fout=new DataOutputStream(fileStream);        //创建数据输出流
            DateFormat dateFormat=DateFormat.getDateTimeInstance();        //创建日期
            fout.writeChars("");                                        //输出空字符
            // fout.writeUTF("生成时间:"+dateFormat.format(new Date()));
            fout.writeUTF(responseWrapper.getContent()); //使用UTF格式输出HTML内容,保存到.html文件中
            fout.close();
        }
        
        System.out.println("/html/"+strs.substring(4)+"");
        request.getRequestDispatcher("/html/"+strs.substring(4)+"").forward(request, response);
        }
        //如果已经生成HTML,直接把请求转发到html文件。
        else{
            System.out.println(""+stres+"");
            request.getRequestDispatcher("/"+stres+"").forward(request, response);
        }
        }
    public void destroy(){
        
    }
    public void log(String msg){
        filterConfig.getServletContext().log(msg);
    }

}


项目目录结构是:

jsp文件夹里放置 list1.jsp 页面 

再创建个html文件夹  放置上面生成的静态页面 list1.html 

访问路径是先走: http://localhost:8090/test/jsp/list1.jsp

访问静态页面路径:http://localhost:8090/test/html/list1.html

达到静态化页面。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值