微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JAVA用于生成静态页面

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

<无详细内容>
package com.lhsoft; 
 import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.sql.ResultSet; import java.sql.sqlException; import java.text.SimpleDateFormat; import java.util.Date; 
 /** 
 *   
 * @author 程志平 
 *  
 * 
 */ public class OperRateAction { 
    private static int PAGESIZE = 10; 
    private static int PAGEINDEX = 10; 
    /** 
     * 该方法用于创建静态页面根据将生成相应页数的页面 
     * @param path 文件存放路径 
     * @param filename 页面文件名 
     * @param statichtml 页面内容 
     * @param i 页面后追加的数字 
     * @param size 每页显示记录条数 
     * @author nickCheng 
     *  
     * */
    private static void createStaticHtml(String path,String filename,StringBuffer statichtml,int i,int size){ 
        if(size > 0){ 
            PAGESIZE = size; 
            PAGEINDEX = size; 
        } 
        int countRow = getRowCount(); 
        int pages = (int)(countRow / PAGESIZE); 
        if(countRow > pages * PAGESIZE) pages = pages + 1; 
          
        String  filepath = "D:"+File.separator+"mytable"+File.separator+"mytable"+i+".html"; 
        if(path != null && !"".equals(path)){ 
            filepath = path+File.separator+filename+i+".html"; 
        } 
        statichtml.append("<tr>"); 
        int j = i-1; 
        int k = i+1; 
        String proPage = filename+j+".html"; 
        String nextPage = filename+k+".html"; 
        if(j == 0) 
            proPage = "javascript:showMsg(0);"; 
        if(k == pages) 
            nextPage = "javascript:showMsg(1);"; 
        int pagess = pages -1; 
        String firstpage = filename+1+".html"; 
        String lastpage = filename+pagess+".html"; 
        statichtml.append("<td colspan='13' style='text-align: center;'><a href='"+firstpage+"'>首页</a>&nbsp;<a href='"+proPage+"'>上一页</a>&nbsp;&nbsp;<a href='"+nextPage+"'>下一页</a>&nbsp;<a href='"+lastpage+"'>最后一页</a>当前为第"+i+"页&nbsp;共"+pagess+"页</td>"); 
        statichtml.append("</tr>"); 
        statichtml.append("</tbody>"); 
        statichtml.append("</table></body></div></html>"); 
        File file =new File(filepath); 
            FileWriter filewriter; 
            try { 
                if(file.exists()){ 
                    file.delete(); 
                    file.createNewFile(); 
                } 
                filewriter = new FileWriter(file,true); 
                filewriter.write(statichtml.toString()); 
                filewriter.close();  
            } catch (IOException e) { 
                e.printstacktrace(); 
            } 
    } 
    /** 
     * 格式化数据 当为日期时将格式为:“yyyy-MM-dd”这种形式 
     * 当出现“null”或者是空字串时替换成“&nbsp;” 
     *  
     * */
    private static String fomatData(Object data){ 
        String  formatdata = null; 
        if(data instanceof Date ){ 
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
            formatdata = format.format(data); 
        } 
        else{ 
            formatdata = (data == null || "".equals(data.toString()))?"&nbsp;":data.toString(); 
        } 
        return formatdata; 
    } 
    /** 
     * 获得记录条数 
     * */
    private static int getRowCount(){ 
        int rowCount = 0 ; 
        OperateDB db = new OperateDB(); 
        String sql = "select count(0) from mytable"; 
        db.Query(sql); 
        ResultSet rs = db.getRs(); 
        try { 
            rs.next(); 
            rowCount = rs.getInt(1);  
        } catch (sqlException e) { 
            e.printstacktrace(); 
        }  
        return rowCount; 
    } 
    /** 
     * 静态页面的拼接 
     * @param path 路径 
     * @param filename 静态文件名 
     * @param size 没有显示记录数 
     * */
    public static boolean publish(String path,String size){ 
        int pagesize= 0 ; 
        if(size !=null && !"".equals("")){ 
            pagesize= Integer.valueOf(size); 
            if(pagesize > 0){ 
                PAGESIZE = pagesize; 
                PAGEINDEX = pagesize; 
            } 
        } 
        OperateDB db = new OperateDB(); 
        String sql = "select * from mytable"; 
        db.Query(sql); 
        StringBuffer statichtml = new StringBuffer(); 
        ResultSet rs = db.getRs(); 
        try { 
            int i = 1; 
            int countRow = getRowCount(); 
            int pages = (int)(countRow / PAGESIZE); 
            if(countRow > pages * PAGESIZE) pages = pages + 1; 
              while(rs.next()) 
              { 
                  int currentrow = rs.getRow(); 
                  if(currentrow <= PAGEINDEX){ 
                      statichtml.append("<tr>"); 
                      statichtml.append("<td>"+fomatData(rs.getString("id"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getDate("orig_time"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getDouble("latitude"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getDouble("longitude"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getDouble("depth"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getString("type_mag"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getDouble("num_mag"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getString("epicenter"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getString("signs"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getDate("intime"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getString("gis_pic"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getString("doc_path"))+"</td>"); 
                      statichtml.append("<td>"+fomatData(rs.getString("sm_content"))+"</td>"); 
                      statichtml.append("</tr>"); 
                      if(currentrow == PAGEINDEX){ 
                          StringBuffer headhtml = new StringBuffer(); 
                            headhtml.append("<html>"); 
                            headhtml.append("<head>"); 
                            headhtml.append("<title>"+filename+"</title>"); 
                            headhtml.append("<Meta http-equiv='Content-Type' content='text/html; charset=gbk'>"); 
                            headhtml.append("<script>"); 
                            headhtml.append("function showMsg(i){if(i==0)alert('对不起,当前页已经是最上一页啦');else alert('对不起,当前页已经是最后一页啦');}"); 
                            headhtml.append("</script>"); 
                            headhtml.append("</head><body>"); 
                            headhtml.append("<div style='text-align:center;'>"); 
                            headhtml.append("<table width='800' border='1' cellspacing=0 cellpadding=0><thead><tr>"); 
                            headhtml.append("<th>ID</th>"); 
                            headhtml.append("<th>ORIGTIME</th>"); 
                            headhtml.append("<th>LATITUDE</th>"); 
                            headhtml.append("<th>LONGITUDE</th>"); 
                            headhtml.append("<th>DEPTH</th>"); 
                            headhtml.append("<th>TYPEMAG</th>"); 
                            headhtml.append("<th>NUMMAG</th>"); 
                            headhtml.append("<th>EPICENTER</th>"); 
                            headhtml.append("<th>SIGNS</th>"); 
                            headhtml.append("<th>INTIME</th>"); 
                            headhtml.append("<th>GISPIC</th>"); 
                            headhtml.append("<th>DOCPATH</th>"); 
                            headhtml.append("<th>SMCONTENT</th>"); 
                            headhtml.append("</thead><tbody>"); 
                          StringBuffer currenthtml = headhtml.append(statichtml); 
                          createStaticHtml(path,filename,currenthtml,i,pagesize); 
                          currenthtml.delete(0,Integer.MAX_VALUE); 
                          statichtml.delete(0,Integer.MAX_VALUE); 
                          i++; 
                          PAGEINDEX = PAGESIZE * i; 
                      } 
                  } 
              }  
              return true; 
        } catch (Exception e) { 
            System.out.println(e.getMessage()); 
            return false; 
        } 
    } 
    /** 
     * @param args 
     */
    public static void main(String[] args) { 
        publish(args[0],args[1],args[2]); 
    } 
  
}

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐