您好,欢迎访问一九零五行业门户网

html实现页面静态化的案例

静态化文件位置注意:
实体类定义:
public class news { private string title; private string pubtime; private string category; private string newscontent; public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getpubtime() { return pubtime; } public void setpubtime(string pubtime) { this.pubtime = pubtime; } public string getcategory() { return category; } public void setcategory(string category) { this.category = category; } public string getnewscontent() { return newscontent; } public void setnewscontent(string newscontent) { this.newscontent = newscontent; } }
自定义流的工具类
public class charstreamio { public void copyfile(string fsrc,string fdest){ file file = new file(fdest); if(file.exists()){ file.delete(); } printwriter out = null; bufferedreader in = null; try { in = new bufferedreader(new filereader(fsrc)); out = new printwriter(new bufferedwriter(new filewriter(fdest))); string strret; while((strret=in.readline()) != null){ out.println(strret); out.flush(); } } catch (exception e) { e.printstacktrace(); }finally{ if(in != null){ try { in.close(); } catch (exception e2) { e2.printstacktrace(); } } if(out != null){ try { out.close(); } catch (exception e2) { e2.printstacktrace(); } } } } /** * 把传入的信息,保存成文件 * @param finfo 传入的文件内容信息 * @param fname 目标路径和文件名 */ public void writefile(string finfo,string fdest){ file file = new file(fdest); if(file.exists()){ file.delete(); } printwriter out = null; try { out = new printwriter(new bufferedwriter(new filewriter(fdest))); out.write(finfo); out.flush(); } catch (exception e) { e.printstacktrace(); }finally{ if(out !=null){ out.close(); } } } /** * 读取文本型文件 * @param name * @return */ public string readfile(string fname){ file file = new file(fname); stringbuilder bild = new stringbuilder(); bufferedreader in = null; if(file.exists()){ try { in = new bufferedreader(new filereader(fname)); string strret; while((strret=in.readline()) != null){ bild.append(strret); } } catch (exception e) { e.printstacktrace(); }finally{ if(in != null){ try { in.close(); } catch (exception e2) { e2.printstacktrace(); } } } }else{ system.out.println(fname + "不存在"); } return bild.tostring(); } }
数据访问层
public class newsdao { /** * 读取数据库中要生成的新闻信息 * @return */ public list<news> getallnews(){ charstreamio io = new charstreamio(); simpledateformat sd = new simpledateformat("yyyy-mm-dd hh:mm:ss"); list<news> newslist = new arraylist<news>(); news n1 = new news(); n1.setcategory("sport"); string c1 = io.readfile("newsinfo\\news1.txt"); n1.setnewscontent(c1); n1.setpubtime(sd.format(new date())); n1.settitle("深足教练组:说我们买球是侮辱 朱广沪常暗中支招"); news n2 = new news(); n2.setcategory("hot"); string c2 = io.readfile("\\newsinfo\\news2.txt"); n2.setnewscontent(c2); n2.setpubtime(sd.format(new date())); n2.settitle("对对对发发发失误失误"); newslist.add(n1); newslist.add(n2); return newslist; } }
业务逻辑层
public class newsbiz { /** * 读取数据库中要生成的新闻信息 * @return */ public void createallnews() throws exception{ newsdao dao = new newsdao(); list<news> newslist = dao.getallnews(); string destpath = "/news/newspages"; for(int i=0;i<newslist.size();i++){ //读取模板 charstreamio io = new charstreamio(); string tmp = io.readfile("/news/news.tmp"); //替换模板中的参数数据 news n = newslist.get(i); string newtmp; newtmp = tmp.replace(templateparam.title, n.gettitle()); newtmp = newtmp.replace(templateparam.category,n.getcategory()); newtmp = newtmp.replace(templateparam.pub_time,n.getpubtime()); newtmp = newtmp.replace(templateparam.content, n.getnewscontent()); //把替换后的内容保存成新闻页面 io.writefile(newtmp, destpath + "/news-" + i + ".html"); } } }
templateparam类
public class templateparam { public static final string title = "%{title}%"; public static final string category = "%{category}%"; public static final string content = "%{newscontent}%"; public static final string pub_time = "%{pubtime}%"; }
用户接口层
public class newstest { public static void main(string[] args) { newsbiz biz = new newsbiz(); try { biz.createallnews(); system.out.println("新闻页面创建完毕!"); } catch (exception e) { e.printstacktrace(); } } }
以上就是html实现页面静态化的案例的详细内容。
其它类似信息

推荐信息