Static HTML files generated in Java are also frequently used. In this summary, spring MVC is used here, and other frameworks have different ideas.
1. Use freemarker to implement the news template.
<HTML>
<Head>
<Title >$ {NEWS. newstitle} </title>
</Head>
<Body>
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
<TD align = "center"> <Table width = "778" border = "0" align = "center" cellpadding = "0" cellspacing = "0">
<Tr>
<TD>
<Table cellspacing = "0" cellpadding = "0" width = "100%">
<Tr>
<TD class = "news_title" align = "center" bgcolor = "# f8f8f8" >$ {NEWS. newstitle} </TD>
</Tr>
<Tr>
<TD Height = "1" bgcolor = "# dddddd"> </TD>
</Tr>
<Tr>
<TD Height = "30" align = "center" bgcolor = "# f8f8f8"> <font color = "# ff3300" >$ {NEWS. newsfrom} </font> & nbsp; & nbsp ;$ {NEWS. newsdate} & nbsp; <font color = "# 0000ff" >$ {NEWS. newsauthor} </font> </TD>
</Tr>
<Tr>
<TD valign = "TOP" class = "p_content" bgcolor = "# f8f8f8" >$ {NEWS. newscontent} </TD>
</Tr>
</Table>
</TD>
</Tr>
</Table> </TD>
</Tr>
</Table>
</Body>
</Html>
2. The insert method of the DaO layer is as follows:
Public void insertnews (News news) throws dataaccessexception {
Templateparam = settemplateparam (News );
// Generate static news
News. setfilename (New makefile (). Make (News, templateparam ));
This. gethibernatetemplate (). Save (News );
/**
* Modify news
*
* @ Param news
*
*/
Public void updatenews (News news) throws dataaccessexception {
Templateparam = settemplateparam (News );
// Update the original static news
New makefile (). Update (News, templateparam );
This. gethibernatetemplate (). Update (News );
}
Private templateparam settemplateparam (News news ){
// Set template parameters
Templateparam = new templateparam ();
Templateparam. setrealpath (news. getrealpath ());
Templateparam. settemplatename ("news. FTL ");
Return templateparam;
}
The templateparam class is defined as follows, mainly to save each additional attribute, such as the Template Name, the path of the template, and the directory for saving news,
Public class templateparam {
Private string realpath;
Private string templatepath;
Private string savedirectory;
Private string filepostfix;
Private string templatename;
Public void insertnews (News news) throws dataaccessexception {
Templateparam = settemplateparam (News );
// Generate static news
News. setfilename (New makefile (). Make (News, templateparam ));
The makefile () method is called to save the generated static file to the disk, and then store it to other properties of the news object using Dao.
Let's look at the makefile function.
Import freemarker. template. configuration;
Import freemarker. template. defaultobjectwrapper;
Import freemarker. template. template;
Import freemarker. template. templateexception;
Public String make (News news, templateparam ){
Configuration CFG = new configuration ();
// Project path
String realpath = templateparam. getrealpath ();
// News template path
String templatepath = realpath + constant. newstemplatepath;
// Store the data in the subdirectory yyyymmdd in the news directory, that is, the data is stored by days.
String cdatestr = "News/" + pureutil. getdateformatstr ("yyyymmdd ");
// File suffix
String filepostfix = ". html ";
// Static news file storage path
String Path = realpath + cdatestr;
string filetimename = pureutil. getdateformatstr ("yyyymmddhhmmss");
// write to the database location path
string returnfilename = cdatestr + "/" + filetimename + filepostfix;
string filename = "";
file newsdir = new file (PATH);
filename = path + "/" + filetimename + filepostfix;
If (! Newsdir. exists () {
newsdir. mkdirs ();
filename = path + "/" + filetimename + filepostfix;
}< br> try {
cfg. setdirectoryfortemplateloading (new file (templatepath);
cfg. setobjectwrapper (New defaultobjectwrapper ();
template newstemplate = cfg. gettemplate (templateparam. gettemplatename ();
newstemplate. setencoding ("GBK");
map root = new hashmap ();
root. put ("news", News);
writer out = new outputstreamwriter (New fileoutputstream (filename);
try {
newstemplate. process (root, out);
}catch (templateexception e) {
E. printstacktrace ();
}< br> out. flush ();
out. close ();
}catch (ioexception e) {
E. printstacktrace ();
}< br> return returnfilename;
}
/**
* Generate an HTML file and use the original file name.
*
* @ Param news
* @ Param templateparam
*/
Public void Update (News news, templateparam ){
Configuration CFG = new configuration ();
// Project path
String realpath = templateparam. getrealpath ();
// News template path
String templatepath = realpath + constant. newstemplatepath;
String filename = News. getfilename ();
// Original path. When the original directory is deleted, the directory is rebuilt.
String Path = realpath + filename. substring (0, filename. lastindexof ("/"));
Filename = realpath + news. getfilename ();
File olddir = new file (PATH );
If (! Olddir. exists ()){
Olddir. mkdirs ();
}
// Note that freemarker is used for processing.
Try {
Cfg. setdirectoryfortemplateloading (new file (templatepath ));
Cfg. setobjectwrapper (New defaultobjectwrapper ());
Template newstemplate = cfg. gettemplate (templateparam. gettemplatename ());
Newstemplate. setencoding ("GBK ");
Map root = new hashmap ();
// Place the news object in the map in freemarker.
Root. Put ("news", News );
Writer out = new outputstreamwriter (New fileoutputstream (filename ));
Try {
Newstemplate. Process (root, out );
} Catch (templateexception e ){
E. printstacktrace ();
}
Out. Flush ();
Out. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}