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

Example Analysis of rendering display of web template data in SpringBoot

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you an example analysis of the rendering and presentation of web template data in SpringBoot. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Template engine

SpringBoot renders the results of the page through the template engine. The main template engines that officially provide preset configuration are

Thymeleaf

FreeMarker

Velocity

Groovy

Mustache

Here we demonstrate the use of Thymeleaf and FreeMarker template engines.

Thymeleaf

Thymeleaf is a modern server-side Java template engine for Web and stand-alone environments.

The main goal of Thymeleaf is to bring elegant natural templates to your development workflow-HTML can be displayed correctly in the browser or work as a static prototype, thus enhancing the collaboration of the development team.

With Spring Framework's modules, extensive integration with your favorite tools, and the ability to plug in your own features, Thymeleaf is ideal for modern HTML5 JVM Web development-although it has more.

Create a new module

Choose the build we need

Spring Boot DevTools in Developer Tools

Spring Web in Web

Thymeleaf in Template Engines

You can also introduce thymeleaf dependencies directly into pom.xml

Org.springframework.boot spring-boot-starter-thymeleaf

Create a template page

Src/main/resources/templates/index.html

Thymeleaf-entry Station

Create controller

Com.rumenz.lession4.controller.ThymeleafRumenController

Package com.rumenz.lession4.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod / * @ className: ThymeleafRumenController * @ description: TODO Class description * @ author: rumenz.com * @ date: 2021-11-1 * * / @ Controller@RequestMapping ("/") public class ThymeleafRumenController {@ RequestMapping (value = "/ index", method= RequestMethod.GET) public String index (ModelMap m) {/ / data can also be queried from the database and returned to m.addAttribute ("name", "entry site") M.addAttribute ("url", "https://rumenz.com"); / / return is a page number: src/main/resources/templates/thymeleaf.html return" thymeleaf ";}})

Start the project

Browser verification

Browsers access http://127.0.0.1:8080/index

FreeMarker

FreeMarker is a template engine, that is, a general tool based on templates and data to be changed, and used to generate output text (HTML pages, email, configuration files, source code, etc.).

Introduce dependency org.springframework.boot spring-boot-starter-freemarker to create templates

Src/main/resources/templates/freemarker.html

Freemarker-entry site FreeMarker case

Name: ${name}

Web site: ${url}

Configuration file

Src/main/resources/application.properties, which specifies the suffix of the template file.

Spring.freemarker.suffix=.html

Write controller

Com.rumenz.lession4.controller.FreeMarkerRumenController

Package com.rumenz.lession4.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping / * @ className: FreeMarkerRumenController * @ description: TODO Class description * @ author: entry site rumenz.com * @ date: 2021-11-1 * * / @ Controller@RequestMapping ("/") public class FreeMarkerRumenController {@ RequestMapping ("/ index2") public String index2 (ModelMap m) {/ / data can also be queried from the database and returned to m.addAttribute ("name", "entry site") M.addAttribute ("url", "https://rumenz.com"); / / return is a page number: src/main/resources/templates/freemarker.html return" freemarker ";}})

Browser verification

Browsers access http://127.0.0.1:8080/index2

The above is an example of the rendering display of web template data in SpringBoot shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis for understanding. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report