Get the App
SLTechnology News&Howtos  ›  Development  › 

How to integrate Freemarker template engine based on SSM

Shulou Source: shulou.com Published: 2022-06-02 00:49:25 10月05日 Update

This article mainly shows you "how to integrate Freemarker template engine based on SSM", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to integrate Freemarker template engine based on SSM" this article.

Introduction to FreeMarker

FreeMarker is a template engine written in the Java language that generates text output based on templates. FreeMarker has nothing to do with the Web container, that is, when Web is running, it does not know about Servlet or HTTP. It can not only be used as a presentation layer implementation technology, but also can be used to generate XML,JSP or Java text and so on.

It can completely separate the presentation layer from the business logic.

In the process of using JSP development, we have found that there are a lot of code with business logic in the page, which makes the content of the page messy, which becomes very difficult in the later stage of a large number of modifications and maintenance.

The principle of FreeMarker is: template + data model = output, the template is only responsible for the performance of the data on the page, does not involve any logic code, and all the logic is handled by the data model. The output that the user finally sees is created after the template and the data model are merged.

Background

Under the general trend of separation of front and rear ends, the dependence and coupling between front end and background should be reduced as much as possible in the process of project development, and ajax should be used for interaction between front end and background as far as possible. however, ajax of the whole station is not conducive to website SEO, so template engine is introduced to minimize the dependence of front end on back end. Under SSM architecture, Thymeleaf template engine is generally recommended under Freemarker,Spring Boot architecture.

SSM integrated Freemarker

1. Introduce dependency

Org.freemarker freemarker 2.3.29

2. Spring mvc core configuration file, spring-mvc.xml, add related configuration

5 UTF-8 UTF-8 yyyy-MM-dd HH:mm:ss HH:mm:ss 0.#### true,false true auto_detect UTF-8

2. Call the template in Controller to render the page effect.

RequestMapping (value = "/ index", method = RequestMethod.GET) public String index (Model model) {model.addAttribute ("fm", "2020"); return "index";}

Call template engine, jump to / WEB-INF/view/index.ftl, pass data, render page

3. The definition template file is named index.ftl.

The home page ${fm} above is all the content of the article "how to integrate Freemarker template engine based on SSM". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Template engine data page content logic front end model article process UTF-8 output business code background file architecture learning help development Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information Xiaomi OPPO Reno Linux