Freemarker is a template engine, a generic tool that generates text output based on a template, written in pure Java. Freemarker is designed to generate htmlweb pages, especially for applications based on MVC Patterns .
The so-called template, is a copy of the basic content has been written, with a fixed format of the document, which vacated or with placeholders identified content, by the user to fill, different users give different data. The placeholder in the template, when the template is run, is parsed by the template engine and replaces the contents of the placeholder part with Dynamic data.
freemarker Span lang= "en-US" >web application framework, and suitable as web application Framework One component freemarker is not related to web containers, that is, when the web is running, It does not know the servlet or HTTP. Not only can it be used as an implementation technique for the presentation layer, but it can also be used to generate xml,jsp or Java text pieces
Although freemarker has some programming capabilities, it is common for Java programs to prepare the data to be displayed by Freemarker generate pages, display prepared data through templates (e.g.)
Separating the presentation layer and the business logic
UseJSPIn the development process in the page a large number of business logic in the code, so that the content of the page messy, in the late a lot of changes in the maintenance process becomes very difficult. Freemarkernot supported at allJavascript code instead of using theElexpression to output presentation data. FreemarkerThe original design was:Templates+Data Model=Output, the template is only responsible for the performance of the data in the page, and does not involve any logic code, and all the logic is handled by the data model. The output that the user eventually sees is created after the template and data model are merged.
Improve development efficiency
in our previous development, we used JSP pages to present the data, the so-called presentation layer. We all know thatjsp in the first execution of the need to convert to a Servlet class, the development phase of the function tuning timely, the need to frequently modify the jsp, Each time the modification is compiled and converted, imagine how much time we wasted in the program compilation in a day. Compared to JSP ,freemarker template technology does not have the problem of compiling and converting, so there is no problem. And in the development process, we do not have to wait for the interface design developers to complete the page prototype, we can then develop the program.
Clear division of Labor
In the past, when using JSP to display data, the programmer is not familiar with interface design technology, and the interface developer is not familiar with programming language. Coordination is difficult, and after using Freemarker, as an interface developer, focus only on creating HTML files, images, and other visualizations of Web pages, regardless of the data, while the program developer focuses on the system implementation and is responsible for preparing the data to be displayed for the page.
in the Spring in simple use:
Configuration file:
<!--freemarker--><beanid= "Freemarker" class= "freemarker.template.Configuration" > < Propertyname= "Templateloader" ref= "Templetloader"/> <propertyname= "defaultencoding" value= "UTF-8" > </property> <propertyname= "NumberFormat" value= "0" ></property></bean>
Code:
Public Template gettemplate (String tableName) {Template template = null;try {template = Freemarker.gettemplate (tableName , Freemarker.getlocale (), ENCODING); return template;} catch (IOException e) {e.printstacktrace (); return null;}} public void list (string ID, httpservletrequest request,httpservletresponse response) {//STEP.1 Gets the configuration parameters for the form based on the table name String Jversion = Cgformfieldservice.getcgformversionbytablename (ID); map<string, object> configs = Configservice.queryconfigs (id,jversion);//STEP.2 get list FTL template path Freemarkerhelper Viewengine = new Freemarkerhelper (); Map<string, object> paras = new hashmap<string, object> ();//STEP.3 Package page Data Loadvars (configs,paras,request) ;//step.4 combined template + data parameter for page presentation string html = viewengine.parsetemplate ("/ITOO/CGFORM/FREEMAKER/AUTOLIST.FTL", paras); try { Response.setcontenttype ("text/html"); Response.setheader ("Cache-control", "No-store"); PrintWriter writer = Response.getwriter (); writer.println (HTML); Writer.flush ();} catch (IOException e) {e.printstacktrace ();}}
FTL Template:
< #setting number_format= "0.#####################" ><! DOCTYPE html> These are the benefits and simple use that freemaker can bring to us. Of course, there are many problems with using Freemaker , such as reading stale data, throwing exceptions, and so on. Therefore, we should choose the time from the actual project needs to choose. similar to the Freemaker:Velocity and other template engines, can be compared to understand.
Freemaker (i) Introduction