FreeMarker development-getting started with freemarker Development
1. Download freemarker. jar
Download it from the official website: http://freemarker.org /.
The downloaded freemarker-2.3.23.tar.gz 3.2 MB contains freemarker. jar, source code, and API documentation.
2. Create a Java Project
1) create a lib directory in the project, Add freemarker. jar to the lib directory, and "Add to build path ".
2) create a new templates directory under src to store template files. Create the example. flt file in the templates directory. The content is as follows:
${example}
3) create a test class FreeMarkerTest under src. The content is as follows:
1 public class FreeMarkerTest {2 public static void main (String [] args) throws IOException, 3 TemplateException {4 Configuration conf = new Configuration (); 5 // set the directory 6 conf for loading the template file. setDirectoryForTemplateLoading (new File ("src/templates"); 7 // set the template to retrieve the data model in 8 conf. setObjectWrapper (new DefaultObjectWrapper (); 9 // create, parse the Template, and Cache 10 template Template = conf. getTemplate ("example. flt "); 11 // prepare the data 12 Map < String, Object> root = new HashMap <String, Object> (); 13 root. put ("example", "Hello World! "); 14 // Merge data with the template 15 template. process (root, new OutputStreamWriter (System. out); 16} 17}
4) Run FreeMarkerTest and the output is as follows:
Hello World!
3. Associate FreeMarker source code with eclipse
Extract the source directory from freemarker-2.3.23.tar.gz and associate it with eclipse to view the source code in development.