Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction and application examples of page static technology Freemarker technology.

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

A brief introduction to FreeMarker

1. The difference between dynamic pages and static pages.

Before entering the topic, I would like to introduce what is a dynamic web page. A dynamic web page is a web programming technology corresponding to a static web page. Static web pages, with the generation of HTML code, the content and display effect of the page will no longer change (unless you modify the page code). On the other hand, this is not the case for dynamic web pages. Although the page code has not changed, the content displayed can change accordingly with time, environment or the results of database operations. In short, dynamic web pages are the integration of basic HTML syntax specifications and advanced programming languages such as java, VB, VC, database programming and other technologies to achieve efficient, dynamic and interactive management of website content and style.

Through the previous introduction, we can get the advantages and disadvantages of dynamic web page and static web page download (here we only consider the website performance-related issues, information security and other issues do not elaborate):

1) static web pages:

A, the content of the static web page is stable, and the page loading speed is fast.

B. there is no database support for static web pages, so there is a large amount of work in the production and maintenance of the website.

C, the interaction of static web pages is poor, which has great limitations.

2) dynamic web pages:

A, good interactivity.

B, the information of the dynamic web page needs to be read from the database, and each side that is opened needs to get the database. If there are a large number of visitors, it will add a great load to the server, thus affecting the running speed of the website.

From the above comparison, it is not difficult to see that in order to improve the performance of the website, as long as we turn dynamic pages into static pages, there will be a significant improvement in running speed, but the problem arises. It is obviously impractical to make all pages static. Is there any way to make our website not only have the interaction of dynamic web pages, but also have the loading speed of static web pages? FreeMarker can achieve such a demand: static dynamic web pages.

2. Download the principle of FreeMarker

FreeMarker is a general tool for integrating templates and data and outputting text based on Java development packages and class libraries. The principle of FreeMarker to achieve page static is to write the style needed in the page into the FreeMarker template file, then dynamically bind the data needed for the page and put it into Map, and then complete the generation of the static page through the FreeMarker template parsing class process () method. Its working principle is shown in figure 2-1.

Template + data model = output

Second, sample demo FreeMarker download

First take a look at the overall structure of the Demo project:

As we have said above, template + data model = output, then we will look at what the template and data model look like, and what the final output looks like.

Note: the syntax of freemarker will be omitted here, because many of them are similar to EL expressions. Instructions for only a few cases are provided here, including list, map, list and map mixed.

FMDemo.java:

1 public class FMDemo {2 3 / / Freemarker 4 public static void main (String [] args) throws Exception {5 6 Configuration conf = new Configuration (); 7 / / template + data model = output 8 / / ftl: freemarker template 9 / / first step: read html template 10 String dir = "C:\ workspace\\ freemarker\\ ftl\"; 11 conf.setDirectoryForTemplateLoading (new File (dir)) 12 Template template = conf.getTemplate ("freemarker.html"); 13 14 / / second step: load the data model 15 Map root = new HashMap (); 16 root.put ("world", "Hello to the world"); 17 18 / / List collection 19 List persons = new ArrayList (); 20 persons.add ("Fan Bingbing") 21 persons.add ("Li Bingbing"); 22 persons.add ("he Bingbing"); 23 root.put ("persons", persons); 24 25 / / Map set 26 Map map = new HashMap (); 27 map.put ("fbb", "Fan Bingbing"); 28 map.put ("lbb", "Li Bingbing") 29 root.put ("map", map); 30 31 / / list and map mixed 32 List maps = new ArrayList (); 33 Map pms1 = new HashMap (); 34 pms1.put ("id1", "Fan Bingbing"); 35 pms1.put ("id2", "Li Bingbing"); 36 Map pms2 = new HashMap () 37 pms2.put ("id1", "Tsang Chi-wai"); 38 pms2.put ("id2", "Ho Chi"); 39 maps.add (pms1); 40 maps.add (pms2); 41 root.put ("maps", maps); 42 43 Writer out = new FileWriter (new File (dir + "hello.html")); 44 template.process (root, out) 45 System.out.println ("build complete"); 46} 47}

Freemarker.html: template file download

12 3 4 5 Insert title here 6 7 8 ${world} 9 10 11 12 13 ${person}-Red 14 15 ${person}-Green 16 17 18 19 20 ${map [key]} 21 22 ${map.fbb} / ${map.lbb} 23 24 25 26 ${map [key]} 27 28 29 30 ${map.id1} / ${map.id2} 31 32 33

Executes the Main method in FMDemo.java, which generates:

Hello.html:

View Code

Third, the use and download of static pages in the project

Here is the static page in the use of the project, now only to the product details page to do static processing.

As mentioned in the previous article on ActiveMQ, when a product is on the shelf, the corresponding page will be static by sending a message to notify babasport-cms.

Here we only write the method of receiving messages. First, let's take a look at the structure diagram of babasport-cms:

CustomMessageListener.java: receive messages in MQ

View Code

StaticPageServiceImpl.java:

View Code

Use Spring to manage Freemarker configuration files:

1 2 3 4 5 6 7 8 9 10 11

Template page: changes in product.html:

Bring in other pages: download

Loop through the colors:

1 2 3

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report