http://herryhaixiao.iteye.com/blog/677524
Because Freemarker this technology a long time to have, comments I did not write very detailed, I believe we can understand. Here's the code and some code explanations.
The Showcourseview class is responsible for the encapsulation of some properties,
Package Test;import Java.io.file;import Java.io.fileoutputstream;import java.io.outputstreamwriter;import Java.io.writer;import java.util.arraylist;import java.util.hashmap;import Java.util.list;import Java.util.Locale; Import Java.util.map;import Pojo. Showcourseview;import Freemarker.template.configuration;import Freemarker.template.template;public Class freemarkertest {Private Configuration Config;public configuration GetConfig () {return config;} /** * Note: setencoding This method must be set the country and its code, otherwise the Chinese in Flt will become garbled after the HTML is generated * * @param filePath * File path * @throws Exception */p ublic void init (String filePath) throws Exception {config = new Configuration (); Config.setdirectoryfortemplateloading ( New File (FilePath)); Config.setencoding (Locale.china, "Utf-8");} /** * Show course data in HTML file with Flt file * * @param filePath * Flt File path * @param templatefile * Flt Template file * @param l IST * To generate the collection data for HTML * @param charset * FLT generated Data encoding format * @param htmlfile * Generate HTML files via FLT * @ ThRows Exception */public void Showcourse (String filePath, String templatefile,list<showcourseview> List, string CharSet, String Htmlfile) throws Exception {init (filePath); map<string, object> root = new hashmap<string, object> (); Template temp = GetConfig (). GetTemplate (TemplateFile); Root.put ("Courselist", list); Writer out = new OutputStreamWriter (new FileOutputStream (Htmlfile), CharSet); Temp.process (root, out);} public static void Main (string[] args) throws Exception {freemarkertest test = new Freemarkertest (); Showcourseview view1 = new Showcourseview () view1.setcategoryname ("categoryName1"); View1.setcoursecode ("Code1"); View1.setname ("name1"); Showcourseview view2 = new Showcourseview () view2.setcategoryname ("categoryName2"); View2.setcoursecode ("Code2"); View2.setname ("name2"); Showcourseview view3 = new Showcourseview () view3.setcategoryname ("CategoryName3"); View3.setcoursecode ("Code3"); View3.setname ("Name3"); list<showcourseview> list = new ARRAYLIST<SHOWCOURSEVIEW≫ (); List.add (View1); List.add (VIEW2); List.add (VIEW3); Test.showcourse ("E:/myproject/freemarker&html/src", " COURSE.FTL ", List," Utf-8 "," course.html ");}}
Package Pojo;public class Showcourseview {private String name; Private String Coursecode; Private String CategoryName = null; Public String Getcategoryname () { return categoryname; } public void Setcategoryname (String categoryname) { this.categoryname = CategoryName; } Public String GetName () { return name; } public void SetName (String name) { this.name = name; } Public String Getcoursecode () { return coursecode; } public void Setcoursecode (String coursecode) { this.coursecode = Coursecode; } }
Course.flt
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
I say the template file used in a few tags, one is < #list > tags, used to know that the struts2 in the JSP is a list iteration label, this and that iteration tag is the same, the key is that the syntax is not the same, the label has a " Courselist ", this courselist is a key that corresponds to the one I placed in the map of the Showcourse method in the Freemarkertest class, and this key is a list collection, Using this list to iterate over each Courseshowview object;< #if > The label is the same as the if label in the custom label, but it is different from the judging syntax. exists from the translation, this is the existence, add a question mark to indicate whether the existence, There is a return of true, there is no return false,row.categoryname?exists judge Showcourseview in the CategoryName whether there is a value, there is a value to display CategoryName, does not show The rest of the syntax such as the value of the problem, the object value problem, this is a lot of attention to the problem, how to take the value, in fact, this is very simple, and El expression in the value method is the same; ${} so you can take a value; Finally, there is a function in <div>. What does this mean by default? Let me tell you something, ShowMore. Displays the value in default if it is empty.
I have 2 Chinese fonts in the Flt file, which is why I set the config encoding for the Init method in the Freemarkertest class. Because when the HTML is generated, if you do not configure the code of the config, it will be encoded with its default encoding, so you can see in the HTML of the Chinese will become garbled (this refers to the garbled text refers to the Chinese in the Flt, not the background generated in Chinese, remember).
OK, the above is the knowledge I used to study freemarker, welcome you to make bricks, write bad don't take offense, write this is because the specific code on the Web is not much, not a more complete, It is the freemarker of the explanation is still quite a lot of, of course, I this is a relatively simple example, if need to deepen understanding, or need to write their own code, I just give everyone a guide role, (*^__^*) hehe ...
Generating HTML using Freemarker