Detailed examples of static Java Freemarker pages

Time:2019-9-8

Freemarker

FreeMarker is a template engine written in Java language, which generates text output based on templates. FreeMarker has nothing to do with Web containers, that is, when the Web runs, it does not know about Servlets or HTTP. It can be used not only as an implementation technology of the presentation layer, but also to generate XML, JSP or Java.
At present, in enterprises, Freemarker is mainly used for static pages or page display.

summaryFreemarker template engine, you can use Freemarker template to generate HTML pages.

Freemarker grammar

/**
   * Freemark introductory case
   * Freemark three elements:
   * 1.freemark API
   * 2. Data
   * 3. Template file: FTL file
   * @throws Exception 
   */
  @Test
  public void test1() throws Exception{
   // Create the FreeMarker core configuration object and specify the FreeMarker
   Configuration cf = new Configuration(Configuration.getVersion());
   // Specify the path of the server template file
   cf.setDirectoryForTemplateLoading(new File("F:\template"));
   // Specify template file encoding
   cf.setDefaultEncoding("utf-8");
   // Get the template file object from under the template file path
   Template template = cf.getTemplate("hello.ftl");

   // Create map objects and encapsulate template data
   Map<String,Object> maps = new HashMap<String,Object>();
   Maps. put ("hello", "freemarker introductory case");
   // Create an output object and output the data to the daoHtml page
   Writer out = new FileWriter(new File("F:\template\out\quickstart.html"));
   // Generate Html pages
   template.process(maps, out);

   // Closing resources
   out.close();
  }

  /**
   * Freemarker Template Syntax Processing Special Data Format 
   * For example: $0.2,20%
   * Template syntax: $0.2: ${price? String. currency}
   * 20%:${price?string.percent}
   * @throws Exception 
   */
  @Test
  public void test2() throws Exception{
   // Create the freemark core configuration object and specify the freemark version
   Configuration cf = new Configuration(Configuration.getVersion());
   // Specify the path of the template file
   cf.setDirectoryForTemplateLoading(new File("F:\template"));
   // Setting Template Coding
   cf.setDefaultEncoding("utf-8");
   // Extract template file objects from the template file path
   Template template = cf.getTemplate("num.ftl");
   // Create map objects and encapsulate template data
   Map<String,Object> maps = new HashMap<>();
   maps.put("price", 0.2);
   // Create output objects and export data to Html pages
   Writer out = new FileWriter(new File("F:\template\out\price.html"));
   // Generate Html pages
   template.process(maps, out);

   // Closing resources
   out.close();
  }

  /**
   * Processing null values using template syntax
   * Template file processing syntax:
   * 1.?
   * Syntax: ${username? Default ("Zhangsan")}
   * 2.!
   * Grammar:
   * ${username!}
   * ${username! "Default value"}
   * 3.if
   * Grammar:
   * <#if username??>
   * ${username}
   * </#if>
   * @throws Exception 
   */
  @Test
  public void test3() throws Exception{
   // Create the freemark core configuration object and specify the FreeMarker version
   Configuration cf = new Configuration(Configuration.getVersion());
   // Specify the path of the template file
   cf.setDirectoryForTemplateLoading(new File("F:\template"));
   // Setting Template Coding
   cf.setDefaultEncoding("utf-8");
   // Getting Template File Objects from Template File Path
   Template template = cf.getTemplate("null.ftl");
   // Create map objects and encapsulate template data
   Map<String,Object> maps = new HashMap<>();
   maps.put("username", null);
   // Create output objects and export data to HTML pages
   Writer out = new FileWriter(new File("F:\template\out\username.html"));
   // Generate HTML pages
   template.process(maps, out);
   // Closing resources
   out.close();
  }

  /**
   * Processing POJO data using template syntax
   * el expression obtains data:
   * model.addAttribute("p",person);
   * ${p.username}
   * ${p.address}
   * Template grammar for POJO data acquisition
   * model.addAttribute("p",person);
   * ${p.username}
   * ${p.address}
   * @throws Exception 
   */
  @Test
  public void test4() throws Exception{
   // Create the freemark core configuration object and specify the FreeMarker version
   Configuration cf = new Configuration(Configuration.getVersion());
   // Specify the path of the template file
   cf.setDirectoryForTemplateLoading(new File("F:\template"));
   // Setting Template Coding
   cf.setDefaultEncoding("utf-8");
   // Getting Template File Objects from Template File Path
   Template template = cf.getTemplate("person.ftl");
   // Create map objects and encapsulate template data
   Map<String,Object> maps = new HashMap<>();
   // Create a person object
   Person person = new Person();
   Person. setUsername ("Zhang San");
   person.setAge(22);
   maps.put("p", person);
   // Create output objects and export data to HTML pages
   Writer out = new FileWriter(new File("F:\template\out\person.html"));
   // Generate HTML pages
   template.process(maps, out);
   // Closing resources
   out.close();
  }

  /**
   * Processing Collection Data with Template Syntax
   * el expression obtains data:
   * model.addAttribute("pList",pList);
   * <c:foreach item="pList" var="p">
   *  ${p.username}
   *  ${p.age}
   * </c:foreach>
   * Template grammar for list data
   * model.addAttribute("pList",pList);
   * <#list pList as p>
   *  ${p.username}
   *  ${p.age}
   * </#list>
   * Corner grammar: ${alias_index}
   * @throws Exception 
   */
  @Test
  public void test5() throws Exception{
   // Create the freemark core configuration object and specify the FreeMarker version
   Configuration cf = new Configuration(Configuration.getVersion());
   // Specify the path of the template file
   cf.setDirectoryForTemplateLoading(new File("F:\template"));
   // Setting Template Coding
   cf.setDefaultEncoding("utf-8");
   // Getting Template File Objects from Template File Path
   Template template = cf.getTemplate("list.ftl");
   // Create map objects and encapsulate template data
   Map<String,Object> maps = new HashMap<>();
   // Create list collection
   List<Person> pList = new ArrayList<>();
   // Create a person object
   Person person1 = new Person();
   Person1. setUsername ("Zhang San");
   person1.setAge(22);
   // Create a person object
   Person person2 = new Person();
   Person2. setUsername ("Li Si");
   person2.setAge(24);
   pList.add(person1);
   pList.add(person2);
   maps.put("pList", pList);
   // Create output objects and export data to HTML pages
   Writer out = new FileWriter(new File("F:\template\out\list.html"));
   // Generate HTML pages
   template.process(maps, out);
   // Closing resources
   out.close();
  }

  /**
   * Template Syntax for Time Type Data Processing
   * Get data date: ${today?Date}
   * Data acquisition time: ${today?Time}
   * Get the date and time of the data: ${today? Datetime}
   * Get data date and time formatting: ${today? String ('yyyy-MM-dd')}
   * @throws Exception 
   */
  @Test
  public void test6() throws Exception{
   // Create the freemark core configuration object and specify the FreeMarker version
   Configuration cf = new Configuration(Configuration.getVersion());
   // Specify the path of the template file
   cf.setDirectoryForTemplateLoading(new File("F:\template"));
   // Setting Template Coding
   cf.setDefaultEncoding("utf-8");
   // Getting Template File Objects from Template File Path
   Template template = cf.getTemplate("date.ftl");
   // Create map objects and encapsulate template data
   Map<String,Object> maps = new HashMap<>();

   maps.put("today", new Date());
   // Create output objects and export data to HTML pages
   Writer out = new FileWriter(new File("F:\template\out\date.html"));
   // Generate HTML pages
   template.process(maps, out);
   // Closing resources
   out.close();
  }

Introducing pages

When introducing another page to this page, you can complete it with the following commands

Jsp Introduce Page:

Ftl Introduce Page: <#include”/include/head.ftl”>

Freemarker Integration Spring

Configuration Integration Freemarker Spring Profile:

<! - Freemarker handed over to spring management - >
 <! - Provide template management configuration objects using spring
 <bean>
  <! - Template Path - >
  <property name="templateLoaderPath" value="/WEB-INF/fm/" />
  <! - Template Coding - >
  <property name="defaultEncoding" value="utf-8" />
 </bean>

Create template objects

Freemarker into the server: WEB-INF folder below: access to resource files, you must start the server.

Load the spring configuration file of application in web.xml:

<! - Load spring MVC - >
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:springmvc.xml,classpath:applicationContext-*.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>

Nginx access

Direct access, can not load style resources, must go through the HTTP server, in order to load static resources.

At this point nginx acts as an HTTP server to access static resources.

Thank you for reading, I hope to help you, thank you for your support on this site!