1. What is page static

Dynamic pages into static HTML, reduce the number of interactions with the database, improve the speed of page access is the server before the request to have fixed things compiled, such as the request to dynamically fill in the data, Don’t wait for the request to come in and do nothing but work your tail off using a template engine provided by a third party to generate the corresponding HTML Thymeleaf Freemark Velocity

2. Why use web static technology

The common thread between web page statics and caching is that web page statics are more suitable for large and relatively infrequently changing data in order to reduce database access pressure. In addition, static web pages are good for SEO. By rendering web pages purely static, you can deploy them using a high-performance Web server such as Nginx, which can handle up to 50,000 concurrent requests, compared to Tomcat’s few hundred

3, Freemarker

3.1. What is a Freemarker

Freemarker is an open source templating engine from Apache that generates text output based on templates. The Freemaker template engine generates static pages from templates and data.

3.2 advantages,

1. Generate a static page based on the template and data in advance and write the page to the hard disk through THE I/O flow. You do not need to access the database and can greatly improve the performance of high concurrent reads to the database. It’s the decrease in database visits.

  1. Because the page is generated in advance, so the access speed is fast, the customer experience is good
  2. Because HTML can be accessed directly by the browser without Tomcat parsing, it reduces the high concurrency pressure on Tomcat.

3.3 Application Scenarios

The rule of freemarker: The page should have a fixed style and be read multiple times at a time, with minimal changes to the data

3.4. Operation process

3.5, templates,

Templates are.ftL files in freemarker. You can use HTML tags in templates, CSS, JS, images and other static resources. You can use EL expressions in templates to fetch data, but you can’t use JSTL tags to judge and loop. So the template engine will have its own set of tag libraries that we can use.

3.6, data

Data is usually stored in a relational database or obtained in Redis or mongodb.Copy the code

3.7. Four elements in a template file

  1. Text, part of the direct output
  2. Comments, i.e. <# –… – > format is not output
  3. Interpolation: ${.. } section, replacing the output with parts from the data model
  4. FTL directive: FreeMarker directive, similar to HTML markup, with # in front of the name to distinguish, no output

4. Getting started

4.1. Create Maven project

4.2. Introduce POM dependencies

< the dependency > < groupId > org. Freemarker < / groupId > < artifactId > freemarker < / artifactId > < version > 2.3.23 < / version > </dependency>Copy the code

Creating a template File

< HTML > <head> <meta charset=" utF-8 "> <title>Freemarker start small DEMO </title> </head> <body> <# ${message} </body> </html>Copy the code

Generate the file

Create a test class

Public class TestFreemarker {/* First step: Create a Configuration object, directly new an object. The constructor takes the version number of Freemarker. Step 2: Set the path for the template file. Step 3: Set the character set used by the template file. Usually utF-8. Step 4: Load a template and create a template object. Step 5: Create a data set to be used by the template, either a POJO or a Map. Usually a Map. Step 6: Create a Writer object, usually a FileWriter object, and specify the name of the file to be generated. Step 7: Call the process method output file of the template object. */ public static void main(String[] args) throws Exception {//1. Create a Configuration class. Configuration =new Configuration(configuration.getVersion ()); / / 2. Set the template directory (absolute path) configuration. SetDirectoryForTemplateLoading (new File("D:\ Java \ Internet class - high Quality \ distributed Project (code) \\code2\\FreeMarkerPro\\ SRC \\main\ resources\ FTL "); / / 3. Set the character set configuration. SetDefaultEncoding (" utf-8 "); / / 4. Load the Template Template Template = configuration. GetTemplate (" MyFreemark. FTL "); Map =new HashMap(); map.put("name", "joker"); map.put("message", "welcome Freemarker"); Writerout =new FileWriter(new File("d:\ test.html")); Template. process(map, out); //8. Close the Writer object out.close(); }}Copy the code

4.3. Click test

Generate file in D:test.html

5. FTL instruction

5.1. Assign directive

This directive is used to define a variable on the page

<#assign myname="Myxq--">
${myname}
Copy the code

Defining object types

< # assign info = {" name ":" myxqName ", "age" : '30'} > phone: ${info. Name} address: ${info. Age}Copy the code

5.2. The list directive

Through the array

List goodsList=new ArrayList(); goodsList.add("goods1"); goodsList.add("goods2"); goodsList.add("goods3"); Map map=new HashMap(); map.put("goodsList",goodsList); Writerout =new FileWriter(new File("d:\ test.html")); Template. process(map, out);Copy the code

<#list users as user>
    ${user_index} --- ${user.name}--${user.age}
</#list>
Copy the code

5.3. The if directive

< # assign myname = "myxq" > < # if myname = "myxq" > < h1 > headline 1 myxq < / h1 > < # else > < h1 > headline 2 < / h1 > < / # if >Copy the code

5.4. Include Directives

Used for nesting template files

 <#include "header.ftl">
Copy the code

conclusion

Like caching, the goal is to reduce the pressure of database access, while web static is suitable for large and relatively infrequently changing data. In addition, static web pages are good for SEO. Rendering a web page in a purely static form allows it to be deployed using a high-performance Web server such as Nginx, which can handle up to 50,000 concurrent requests, compared to Tomcat’s few hundred, which is Freemark’s advantage.