Web effects phttp://www.111cn.net/Web effects p.html target=_blank >jsp tutorials generate static page code
Buildhtml.java:
Import java.util.*;
Import java.io.*;
public class htmlfile{
public static void Main (string[] args) {
try{
string title= ' test ';
String content = "Test"
String editer= "";
string filepath = "";
filepath = "template.html";
System.out.print (filepath);
string templatecontent= "";
FileInputStream FileInputStream = new FileInputStream (filepath);//read template file
int lenght = Fileinputstream.available ();
Byte bytes[] = new Byte[lenght];
Fileinputstream.read (bytes);
Fileinputstream.close ();
TemplateContent = new string (bytes);
System.out.print (TemplateContent);
Templatecontent=templatecontent.replaceall ("# # #title ###", title);
Templatecontent=templatecontent.replaceall ("# # #content ###", content);
Templatecontent=templatecontent.replaceall ("# # #author ###", editer);//Replace the appropriate place in the template
System.out.print ( TemplateContent);
According to the time file name
Calendar calendar = Calendar.getinstance ();
String fileame = String.valueof (Calendar.gettimeinmillis ()) + ". html";
Fileame = "/" + fileame;//the generated HTML file save path.
FileOutputStream FileOutputStream = new FileOutputStream (fileame);//create file output stream
System.out.print ("File Output path:");
System.out.print (Fileame);
byte tag_bytes[] = Templatecontent.getbytes ();
Fileoutputstream.write (tag_bytes);
Fileoutputstream.close ();
}catch (Exception e) {
System.out.print (E.tostring ());
}
}
}
%>
Template file
<title>${title} </title>
<body>
${content}
</body>
Another method Freemarker
Using Freemarker is the principle:
1. Get the template: That is, you have to write a template sample beforehand. Call this template again, just read it from the cache,
no longer instantiate the template template temp = cfg.gettemplate ("TEST.FTL");
GetTemplate method.
2. Combine templates with data models: Data Model + template = output, and once we have the data model (root) and a stencil
(template) then we can merge them to get output.
The above procedure is implemented through the process method of the template class, which requires two parameters, one is
to represent the writer of the data model as the root of the output. It outputs the parsed file to the writer. As simple as
See, I assigned the output to the console (standard output)
writer out = new OutputStreamWriter (system.out);
Temp.process (Root, out);
Out.flush ();