Java Bean:
package com.lee.freemark;public class User {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}
Template file:
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML>
Test class:
Package COM. lee. freemark; import Java. io. bufferedwriter; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. outputstreamwriter; import Java. io. writer; import Java. util. hashmap; import Java. util. map; import freemarker. template. configuration; import freemarker. template. template; import freemarker. template. templateexception; public class freemarktest {/*** freemark Test * @ Param ARGs * @ throws ioexception * @ throws templateexception */public static void main (string [] ARGs) throws ioexception, templateexception {system. out. println ("freemark test started ...... "); configuration Config = new configuration (); string templatepath = thread. currentthread (). getcontextclassloader (). getresource ("template "). getpath (). substring (1); config. setdirectoryfortemplateloading (new file (templatepa Th); config. setsetting ("defaultencoding", "UTF-8"); Template temp = config. gettemplate ("user. FTL "); Map <string, Object> datas = new hashmap <string, Object> (); User user = new user (); User. setname ("millennial step"); datas. put ("Web", "www.ittools.cn"); datas. put ("user", user); string outpath = system. getproperty ("user. dir ") +"/webcontent/"; file OUTFILE = new file (outpath +" user.html "); writer out = new bufferedwriter (New outputstreamwriter (New fileoutputstream (OUTFILE); temp. Process (datas, out); system. Out. println ("freemark test ended! "); // System. out. println (thread. currentthread (). getcontextclassloader (). getresource (""). getpath (). substring (1); // system. out. println (thread. currentthread (). getcontextclassloader (). getresource ("template "). getpath (). substring (1); // system. out. println (freemarktest. class. getresource (""). getpath (). substring (1 ));}}