Static file location Note:
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/A4/FF/wKioL1m2KwSD3R_pAAAjiwpj0zM659.png "title=" Htmlstatic.png "alt=" Wkiol1m2kwsd3r_paaajiwpj0zm659.png "/>
Entity class Definition:
public class News {private string Title;private string Pubtime;private string category;private string Newscontent;public S Tring 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;}}
Tool classes for custom streams
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 ();}}} /** * to save incoming information to file * @param finfo incoming file content * @param fname destination path and file name */public voiD writefile (string finfo,string fdest) {file file = new file (fDest); 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 ();}}} /** * reading text files * @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 + "does not Exist");} return bild.tostring ();}}
Data Access Layer
public class Newsdao {/** * reads the news information to be generated in the database * @return */public list<news> getallnews () {Charstreamio io = new Charstre Amio (); 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 ("Deep-legged coach group: said we buy the ball is insulting Zhu Guanghu often secret weapon"); 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 ("Error on the right to the hair"); Newslist.add (N1); Newslist.add (n2); return newslist;}}
Business Logic Layer
public class newsbiz {/** * read the news information to be generated in the database * @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++) { //Read template Charstreamio io = new charstreamio (); String tmp = io.readfile ("/news/news.tmp");//replace the parameter data in the template 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 ());// Save the replaced content as a news page Io.writefile (newtmp, destpath + "/news-" + i + ". html");}}
Templateparam class
public class Templateparam {public static final string TITLE = "%{title}%";p ublic static final String CATEGORY = "%{catego ry}% ";p ublic static final String CONTENT ="%{newscontent}% ";p ublic static final String pub_time ="%{pubtime}% ";}
User interface Layer
public class Newstest {public static void main (string[] args) {newsbiz biz = new newsbiz (); try {biz.createallnews (); SYSTEM.OUT.PRINTLN ("News page created! ");} catch (Exception e) {e.printstacktrace ();}}}
This article from "Qin Bin blog" blog, declined reprint!
Static implementation of HTML pages