One, foreword

Springboot does not support JSP by default and uses the Thymeleaf template engine by default. So here is an introduction to SpringBoot and Thymeleaf, the implementation of template instances and the problems encountered on the way.

Two, configuration and use

1. Introduce the JAR. Add the starter dependency for Thymeleaf to the POM.

<! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>Copy the code

2. Set Thymeleaf template parameters in the application-dev.yml configuration file.

Spring: thymeleaf: cache: false mode: LEGACYHTML5 prefix: classpath:/templates/ suffix:.htmlCopy the code

Explanation of important parameters:

Cache: Whether to cache or not. Set this parameter to false in development mode to avoid restarting the server after changing the template. Set this parameter to true online to improve performance. This is usually false.

Mode: Configures the view template type. If html5 is used, you need to configure it to HTML5.

Prefix: specifies the directory where the template resides.

Suffix: template suffix.

3. Write a Freemarker template file:

<! DOCTYPE HTML> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta content="text/html; Charset =UTF-8"/> </head> <body> <h6>Thymeleaf template engine </h6> <table border="1" bgcolor="# f0FFFF "> <thead> <tr> <th> < th > log content < / th > < th > IP number < / th > < th > create time < / th > < / tr > < thead > <! --th:each --> <tbody th:each="logList: ${list}"> <tr> <td th:text="${logList .id}"></td> <td th:text="${logList .content}"></td> <td th:text="${logList .ip}"></td> <td th:text="${logList .createTime}"></td> </tr> </tbody> </table> </body> </html>Copy the code

Attach:

Thymeleaf grammar:

Official documentation is a good way to learn, and here I found a PDF Thymeleaf document.

Thymeleaf official documentation PDF version download link: www.thymeleaf.org/doc/tutoria…

Take a look at Thymeleaf’s usage syntax:

4. Next write a Controller and put some data in it.

package com.system.xiaoma.controller; import com.system.xiaoma.entity.LogInfo; import com.system.xiaoma.service.ILogInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; */ @controller @requestMapping ("/page") public class ThymeleafController {@autoWired private ILogInfoService logInfoService; @requestMapping ("/ loglist.html ") public String getArticles(Model Model) {List<LogInfo> List = logInfoService.list(); // Add data to model.adDattribute ("list", list); return "logList"; }}Copy the code

Note:

1, among them, you can return the returned is a relative path, such as your template path in resources/templates/logList HTML; Since you have configured the root path in application-dev.yml, you only need to return to the relative path.

2. Note that only the @controller annotation can be used; If you use @restController directly, you will automatically return the result as a string.

To show you how this page would look if you used the @RestController annotation.

The string returned by return is printed.

Using the @Controller correct demonstration results page, as shown below, indicates that the data was successfully fetched and rendered for display.

Iii. Precautions:

Note that you may encounter this type of problem along the way: 500 errors

Error resolving template [log/logList], template might not exist or might not be accessible by any of the configured Template Resolvers

The static template page has a syntax error and the template parser cannot access it.

Solution:

Step 1: Check that the application-dev.yml file is configured correctly for the page template, and create a templates folder under Resources to put the page in.

Step 2: In controller; Simply return “/logList”.

Well, that’s all for Springboot Thymeleaf to implement static page access. See you next time.

Previous hot articles recommended:

  • What is the experience of developing with IDEA on Ubuntu? What a surprise… Surprise at the end!
  • 520 night I spent 288 ocean to get small teacher younger sister favour, the reason is unexpectedly… Make sure to see it to the end!
  • You’ve never used a code generation tool, but it’s up to you
  • How to implement Springboot project to save local system log files, super detailed, you deserve to have!
  • Use MultipartFile implement image specified path upload download and use the postman | testing tutorial with complete source code, it is strongly recommended that the collection!
  • One article with you zero foundation play mysql trigger | super dry, suggested that collection
  • .

OK, that’s where I’ll start today’s article. If the problem also please timely criticism correction.

If this article has been helpful, please put your thumb in the bottom left corner of the article. (# ^. ^ #);

If you like the articles shared by Bug bug, please send bug bug a note! The danjun ‘ᴗ, you guys will have a cameo appearance with you.

If you have any questions about this article, please leave a comment below or join the group [Group number: 708072830].

In view of limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (no offensive comments, thank you);

Copyright notice: This article is the blogger’s original article, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate! (* ^ del ^ *).