SoFunction
Updated on 2025-03-09

Detailed explanation of java  Freemarker page static instance

Freemarker

FreeMarker is a template engine written in Java language that generates text output based on templates. FreeMarker has nothing to do with web containers, i.e. when the web is running, it does not know about Servlets or HTTP. It can not only be used as an implementation technology for presentation layers, but also for generating XML, JSP, or Java, etc.
Currently in enterprises: Freemarker is mainly used for static pages or page displays

Summarize: freemarker template engine, you can use Freemarker templates to generate html pages.

Freemarker syntax

 /**
    * Freemark Introduction Case
    * Freemark three elements:
    * API
    * 2. Data
    * 3. Template file: ftl file
    * @throws Exception
    */
  @Test
  public void test1() throws Exception{
   //Create a freemarker core configuration object and specify a freemarker   Configuration cf = new Configuration(());
   //Specify the path of the server template file   (new File("F:\\template"));
   //Specify template file encoding   ("utf-8");
   //Get the template file object from the template file path   Template template = ("");

   //Create map object and encapsulate template data   Map<String,Object> maps = new HashMap<String,Object>();
   ("hello", "Freemarker Beginner Case");
   //Create an output object and output the data to daoHtml page   Writer out = new FileWriter(new File("F:\\template\\out\\"));
   //Generate HTML page   (maps, out);

   //Close resources   ();
  }

  /**
    * freemarker template syntax processing special data format
    *Example: $0.2,20%
    * Template syntax: $0.2:${price?}
    * 20%:${price?}
    * @throws Exception
    */
  @Test
  public void test2() throws Exception{
   //Create a freemark core configuration object and specify the freemark version   Configuration cf = new Configuration(());
   //Specify the path to the template file   (new File("F:\\template"));
   //Set template encoding   ("utf-8");
   //Get the template file object from the template file path   Template template = ("");
   //Create map object and encapsulate template data   Map<String,Object> maps = new HashMap<>();
   ("price", 0.2);
   //Create the output object and output the data to the HTML page   Writer out = new FileWriter(new File("F:\\template\\out\\"));
   //Generate HTML page   (maps, out);

   //Close resources   ();
  }

  /**
    * Use template syntax to process null values
    * Template file processing syntax:
    * 1.?
    * Syntax: ${username?default("Zhang San")}
    * 2.!
    * grammar:
    * ${username!}
    * ${username!"Default value"}
    *
    * grammar:
    * <#if username??>
    * ${username}
    * </#if>
    * @throws Exception
    */
  @Test
  public void test3() throws Exception{
   //Create a freemark core configuration object and specify the freemarker version   Configuration cf = new Configuration(());
   //Specify the path to the template file   (new File("F:\\template"));
   //Set template encoding   ("utf-8");
   //Get the template file object from the template file path   Template template = ("");
   //Create map object and encapsulate template data   Map&lt;String,Object&gt; maps = new HashMap&lt;&gt;();
   ("username", null);
   //Create the output object and output the data to the html page   Writer out = new FileWriter(new File("F:\\template\\out\\"));
   //Generate html page   (maps, out);
   //Close resources   ();
  }

  /**
    * Use template syntax to process pojo data
    * el expression gets data:
    * ("p",person);
    * ${}
    * ${}
    * Template syntax to get pojo data
    * ("p",person);
    * ${}
    * ${}
    * @throws Exception
    */
  @Test
  public void test4() throws Exception{
   //Create a freemark core configuration object and specify the freemarker version   Configuration cf = new Configuration(());
   //Specify the path to the template file   (new File("F:\\template"));
   //Set template encoding   ("utf-8");
   //Get the template file object from the template file path   Template template = ("");
   //Create map object and encapsulate template data   Map&lt;String,Object&gt; maps = new HashMap&lt;&gt;();
   //Create a person object   Person person = new Person();
   ("Zhang San");
   (22);
   ("p", person);
   //Create the output object and output the data to the html page   Writer out = new FileWriter(new File("F:\\template\\out\\"));
   //Generate html page   (maps, out);
   //Close resources   ();
  }

  /**
    * Use template syntax to process collection data
    * el expression gets data:
    * ("pList",pList);
    * <c:foreach item="pList" var="p">
    * ${}
    * ${}
    * </c:foreach>
    * Template syntax to get list data
    * ("pList",pList);
    * <#list pList as p>
    * ${}
    * ${}
    * </#list>
    * Angle tag syntax: ${alias_index}
    * @throws Exception
    */
  @Test
  public void test5() throws Exception{
   //Create a freemark core configuration object and specify the freemarker version   Configuration cf = new Configuration(());
   //Specify the path to the template file   (new File("F:\\template"));
   //Set template encoding   ("utf-8");
   //Get the template file object from the template file path   Template template = ("");
   //Create map object and encapsulate template data   Map&lt;String,Object&gt; maps = new HashMap&lt;&gt;();
   //Create list collection   List&lt;Person&gt; pList = new ArrayList&lt;&gt;();
   //Create a person object   Person person1 = new Person();
   ("Zhang San");
   (22);
   //Create a person object   Person person2 = new Person();
   ("Li Si");
   (24);
   (person1);
   (person2);
   ("pList", pList);
   //Create the output object and output the data to the html page   Writer out = new FileWriter(new File("F:\\template\\out\\"));
   //Generate html page   (maps, out);
   //Close resources   ();
  }

  /**
    * Use template syntax to process time type data
    * Get data date: ${today?date}
    * Get data time: ${today?time}
    * Get data date and time: ${today?datetime}
    * Get data date and time format: ${today?string('yyyy-MM-dd')}
    * @throws Exception
    */
  @Test
  public void test6() throws Exception{
   //Create a freemark core configuration object and specify the freemarker version   Configuration cf = new Configuration(());
   //Specify the path to the template file   (new File("F:\\template"));
   //Set template encoding   ("utf-8");
   //Get the template file object from the template file path   Template template = ("");
   //Create map object and encapsulate template data   Map&lt;String,Object&gt; maps = new HashMap&lt;&gt;();

   ("today", new Date());
   //Create the output object and output the data to the html page   Writer out = new FileWriter(new File("F:\\template\\out\\"));
   //Generate html page   (maps, out);
   //Close resources   ();
  }

Introduce a page

When introducing another page to this page, you can use the following command to complete it.

Jsp Introduce Page:

Ftl introduction page: <#include "/include/">

Freemarker integration spring

Configure the integration of Freemarker spring configuration files:

&lt;!-- freemarkerLeave it tospringmanage --&gt;
 &lt;!-- usespring提供模板manage配置对象 --&gt;
 &lt;bean class=""&gt;
  &lt;!-- Template path --&gt;
  &lt;property name="templateLoaderPath" value="/WEB-INF/fm/" /&gt;
  &lt;!-- Template encoding --&gt;
  &lt;property name="defaultEncoding" value="utf-8" /&gt;
 &lt;/bean&gt;

Create a template object

Freemarker Put into the server: Under the WEB-INF folder: To access the resource file, the server must be started.

In the spring configuration file loading application:

&lt;!-- loadspringmvc --&gt;
  &lt;servlet&gt;
  &lt;servlet-name&gt;springmvc&lt;/servlet-name&gt;
  &lt;servlet-class&gt;&lt;/servlet-class&gt;
  &lt;init-param&gt;
   &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
   &lt;param-value&gt;classpath:,classpath:applicationContext-*.xml&lt;/param-value&gt;
  &lt;/init-param&gt;
  &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;

Nginx access

Direct access cannot load style resources. You must go through the http server to load static resources.

At this time, nginx serves as the http server to access static resources.

Thank you for reading, I hope it can help you. Thank you for your support for this site!