Beginner nothing can be ignored is the official website of this thing: http://freemarker.org/. Download or API can refer to here.
What is Freemarker?
Very simple and clear. Freemarker is a Java package. Implemented the MVC pattern, separating developers and designers. This is the code that separates HTML from Java.
How the Freemarker works
This is a map of the official website. It's very clear how Freemarker works. First, you need to have a template, which is a bunch of HTML tags and some exclusive freemarker tags, which will be replaced by specific data. This data is available on the lower left Java objects. <FreeMarker> is the jar package in the project. He put these templates and Java objects data processing after the output of the HTML page. Therefore, Freemarker function, as the official website has been emphasized, not the other but a template engine.
At the end of the project, we put these together so that they know each other and work together with the Web. XML configuration file. This first introduces the configuration of Web. XML, and then explains how to add the template, Java objects, and so on in a step-by-step configuration file. This allows the reader to gradually grasp each of the relevant details from the overall point of view.
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "http://java.sun.com/xml/ns/ Java ee " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xsi:schemalocation=" http://java.sun.com/ Xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <servlet> < Servlet-name>test</servlet-name> <servlet-class>example. test</servlet-class> </servlet> <servlet-mapping> <servlet-name>test</ servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> < welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
The configuration follows: There is a servlet called test that corresponds to the test class of the example package in the code. The name of this class can be random, other nodes in the configuration of the use of knowledge servlet-name, and then he will find this class. The following servlet-mapping accesses *.do in the URL to access the class configured for the Servlet-name node of test. After that, add a hyperlink to the *.do in index.jsp. The entire test project is ready to run.
Understood in terms of MVC. The class configured in Servlet-name is the controller. View is the template. The model is the data that follows in the HashMap. The following sequence creates code that you need directly or indirectly in the configuration file.
Add code that can be run
This assumes that you have created a Web project that is named Test. Create the template first. Be sure to create a templates directory under the WebRoot directory. Then add the suffix to the FTL (freemarker template abbreviation) and the template named Test. There is no template called in the configuration file, so here you can name the template file. This file is then explicitly specified in the controller. There are no problems that cannot be found.
Code:
<! DOCTYPE html>
This template is very simple. Only the H1 label China has a freemarker tag that requires the data "user". This "user" data is provided in the later HashMap.
Then add the controller, which is the Example.test class specified in the Web. XML configuration file. Start by creating a "example" package at the root of the project, and then add a servlet inside it. The name of the servlet is set to test. The name can be random, but it must be consistent with the name of the class in the configuration file.
Package Example;import Java.io.*;import Java.util.*;import javax.servlet.servletexception;import javax.servlet.http . *;import freemarker.template.*;p ublic class Test extends HttpServlet {private Configuration _config; public void init () {//Initialize Freemarker configuration _config = new configuration (CONFIGURATION.VERSION_2_3_0); Set the location of the Freemarker template file _config.setservletcontextfortemplateloading (This.getservletcontext (), "templates"); } public void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexception{ Data Map root = new HashMap (); Root.put ("User", "Jack"); Get template Templates T = _config.gettemplate ("TEST.FTL"); Prepare the output, using the template encoding as the CharSet Response.setcontenttype on this page ("text/html; charset= "+ t.getencoding ()); PrintWriter out = Response.getwriter (); try{//Add Dynamic Data t.process (root, out) in the template; }catch (Freemarker.template.TemplateException e) {throw new Servletexception ("Error in processing template template", e); } }}Code _config.setservletcontextfortemplateloading (This.getservletcontext (), "templates"); Specifies the directory where the template files are located, all future controller template files are found from this place, that is, the previously specified templates directory created under Webroot. In the following code Template t = _config.gettemplate ("TEST.FTL"); Specifies the specific template file. Map root = new HashMap (); Root.put ("User", "Jack"); A specific data model is specified. This key value must be the same as the ${user} set in the template. Finally in method t.process (root, out); , add the data to the template. After these steps, several parts of MVC are combined.
Finally, add a hyperlink in index.jsp:<a href= "hello.do" >freemarker first page</a>. The entire example is ready to run. Look at the effect.
Freemarker is a very powerful template engine. There are not only simple data replacements, but also instructions, macros, and other functions that are described in this article. Very useful.
Reference:
http://freemarker.org/docs/
http://gqsunrise.iteye.com/blog/1659118
Configuration and use of Java:freemarker