Freemark simple example

Source: Internet
Author: User

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 ));}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.