cool hit counter Use of freemarker_Intefrankly

Use of freemarker

I: What is freemarker?

freemarker is a template engine that generates the corresponding text (HTML, xml, java, etc.) based on the defined template and data.

2: Use of freemarker approach

(1) Introduce freemarker-related dependencies in the project


(2) Steps for use

Step 1: Create a Configuration object, just new one directly, the construction parameter is the version number of freemarker

Step 2: Set the path where the template file is located, you need to give the full path where it is stored on the disk

Step 3: Set the encoding format of the generated file, usually utf-8 format

Step 4: Load the template and create the template object

Step 5: Create the dataset used by the template, which can make a pojo or a map type

Step 6: Create a Write stream object to output the document file, you need to specify the name of the generated file

Step 7: Call the process method of the template to generate the appropriate text

Step 8: Close the stream

    public void genFile() throws Exception {
         // Step 1: Create a Configuration object, directly new an object. The argument to the constructor is the version number that freemarker is for.
        Configuration configuration = new Configuration(Configuration.getVersion());
         // Step 2: Set the path where the template file is located.
        configuration.setDirectoryForTemplateLoading(new File("D:/workspace/item-web/src/main/webapp/WEB-INF/ftl"));
         // Step 3: Set the character set to be used in the template file. Usually it's utf-8.
         // Step 4: Load a template and create a template object.
        Template template = configuration.getTemplate("hello.ftl");
         // Step 5: Create a dataset to be used by the template, either pojo or map. It is generally Map.
        Map dataModel = new HashMap<>();
         // Adding data to the data set
        dataModel.put("hello", "this is my first freemarker test.");
         // Step 6: Create a Writer object, generally a FileWriter object, specifying the name of the generated file.
        Writer out = new FileWriter(new File("D:/temp/out/hello.html"));
         // Step 7: Call the process method of the template object to output the file.
        template.process(dataModel, out);
         // Step 8: Close the stream.

(3) Syntax of the template

1.Access the key in the map

${key} to get the corresponding value

2.Accessing properties in pojo

withstudent Object as an example:${} ${} You can getstudent object in theid value andname happen to

3.Go to the elements in the set

For example: iterate through the set of student objects and take out the id and name values of each student

<#list studentList as student>

  ${student,id} ${}


4.Takes the subscript in the set

<#list studentList as student>

   object + underscore + index to get the subscript value




<#list sutdnetList as student>

   Making parity determinations

  <#if student_index % 2 ==0>

     Processing of // even numbers


     //Odd number processing



6.Formatting of date types

${date?date} Current date

${date?time} Current time

${date?datetime} Current date and time

${date?string("yyyy-MM-dd hh:mm:ss")} Setting the date format

7.Handling of NULL

! Control handling of output, output only, no return value

${name} will report an error if name is empty

${name!} If name is empty, no error is reported and no output

${name!" default value'} in casename empty, will output the default value

${name!666} will output 666 if name is empty

${} reports an error if student or name is empty

${!" default value"} in casestudent empty, It will report an error.,name empty, Output default value

What? Tests for null, returning a Boolean type value

product.color? will only test if color is null

 (product.color)? will test product and color for the presence of null 

What? and? differences

?? is to determine if the object is empty, for example:<#if object??>object Object is not empty( i.e.object presence)</#if>

? followed by the name of the built-in function, for example:<#if object?exists>object Object is not empty( i.e.object presence)</#if>

<#if str??>${str?string}</#if><#-- willstr Display as a string-->

8.include tag

<#include “ Template Name“>

III: integration of freemarker and spring

(1) the creation of the Configuration object to spring unified management, the object to inject two properties, the template path and the file encoding format

<bean id="freemarkerConfig"
        <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
        <property name="defaultEncoding" value="UTF-8" />

(2) Conducting the test

public class HtmlGenController {
    private FreeMarkerConfigurer freeMarkerConfigurer;

    public String genHtml()throws Exception {
// 1. Get the FreeMarkerConfigurer object from the spring container.
         // 2. Get the Configuration object from the FreeMarkerConfigurer object.
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
         // 3. Use the Configuration object to get the Template object.
        Template template = configuration.getTemplate("hello.ftl");
         // 4. Creating data sets
        Map dataModel = new HashMap<>();
        dataModel.put("hello", "1000");
         // 5. Create the Writer object for the output file.
        Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html"));
         // 6. Call the process method of the template object to generate the file.
        template.process(dataModel, out);
         // 7. Close the stream.
        return "OK";

I hope you'll correct me if I'm wrong.

1、SparkSql connection to mysql
2、Open Source and Standards Organizations Collide as ETSINFV Working Group and OPNFV Agree to Collaborate
3、YearEnd RoundupTop 10 VR Keywords of 2016 II
4、Deep Learning VI keras common functions to learn 2018 latest win10 install tensorflow 14 GPUCPU cuda80 cudnn80v6 keras install CUDA fails to import ten
5、Telecom scam eats college students are young lives wronged

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送