Although many developments now adopt a completely separate model of front-end and back-end, that is, the back-end only provides data interfaces, and the front-end obtains data through AJAX requests, which does not require a template engine. The advantage of this method is that the front and back ends are completely separated, and with the improvement of front-end engineering tools and MVC frameworks in recent years, the maintenance cost of this model is relatively lower. However, this mode is not conducive to SEO and will be slightly worse in performance. There are also some scenarios that using the template engine will be more convenient, such as email templates. This article mainly discusses the integration of Spring boot with template engines Thymeleaf, Freemaker and JSP.
1. Integrate Thymeleaf
Step 1: Introduce the jar package (starter corresponding to thymeleaf):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
Step 2: Configure thymeleaf:
spring: thymeleaf: prefix: classpath:/templates/ check-template-location: true cache: false suffix: .html encoding: UTF-8 content-type: text/html mode: HTML5
prefix: Specify the directory where the template is located
check-tempate-location: Check whether the template path exists
cache: Whether to cache, set to false in development mode, avoid changing the template and restarting the server. Setting it to true online can improve performance.
encoding&content-type: Everyone should be familiar with this, which is consistent with the corresponding attributes set in Servlets.
mode: Please refer to the instructions on the official website, and this is 2.X and 3.0. The package automatically introduced in this article is 2.15.
The third step is to write the thymeleaf 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 bgcolor="#f0ffff"> <thead> <th>Serial Number</th> <th>Title</th> <th>Summary</th> <th>Creation Time</th> </tr> </tour> <tbody th:each="article : ${list}"> <tr> <td th:text="${article.id}"></td> <td th:text="${article.title}"></td> <td th:text="${article.summary}"></td> <td th:text="${article.createTime}"></td> </tr> </tbody></table></body></html>As you can see, thymeleaf is relatively simple, and its biggest feature is that the tag exists as an attribute of an HTML element. That is to say, the page can be previewed directly through the browser, but there is no data. This is very convenient for everyone to debug.
Step 4: Configure Controller:
@Controller@RequestMapping("/article")public class ArticleController { @Autowired private ArticleService articleService; @RequestMapping("/articleList.html") public String getArticleList(Model model, String title, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) { int offset = (pageNum - 1) * pageSize; List<Article> list = articleService.getArticles(title, 1L, offset, pageSize); model.addAttribute("list", list); return "article/articleList"; }}Note that the annotation used here is @Controller, not @RestController, because @RestController will automatically convert the return result into a string.
Step 5 View the results
2. Integration between Spring boot and Freemarker
1. Introduce jar package (starter corresponding to Freemarker)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId></dependency>
2. Configure freemarker:
spring: freemarker: template-loader-path: classpath:/templates/ suffix: .ftl content-type: text/html charset: UTF-8 settings: number_format: '0.##'
In addition to settings, other configuration options are similar to thymeleaf. settings will have an impact on some behaviors of freemarkers, such as date formatting, number formatting, etc. Interested students can refer to the instructions provided by the official website: https://freemarker.apache.org/docs/api/freemarker/template/Configuration.html#setSetting-java.lang.String-java.lang.String-
3. Write freemarker template file:
<html> <title>Article List</title><body><h6>Freemarker Template Engine</h6> <table> <thead> <tr> <th>Serial Number</th> <th>Title</th> <th>Summary</th> <th>Creation Time</th> </tr> </thead> <#list list as article> <tr> <td>${article.id}</td> <td>${article.title}</td> <td>${article.summary}</td> <td>${article.createTime?string('yyyy-MM-dd hh:mm:ss')}</td> </tr> </#list> </table></body></html>4. Write Controller:
@Controller@RequestMapping("/article")public class ArticleController { @Autowired private ArticleService articleService; @RequestMapping("/list.html") public String getArticles(Model model, String title, @RequestParam(defaultValue = "10") Integer pageSize, Integer pageNum) { if (pageSize == null) { pageSize = 10; } if (pageNum == null) { pageNum = 1; } int offset = (pageNum - 1) * pageSize; List<Article> list = articleService.getArticles(title, 1L, offset, pageSize); model.addAttribute("list", list); return "article/list"; }}5. Visit the page:
3. Sring boot integrates with JSP:
In formal project development, jsp templates are rarely used, so Spring boot supports jsp not very good, so it is relatively more complicated to configure than thymeleaf and Freemaker.
The first step is to introduce the jar package:
<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId></dependency><dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId></dependency>
The first jstl dependency is used to support el expressions, and the second one is used to support jsp. Note that if it is running in external tomcat, you need to set scope to provide to prevent jar package conflicts.
Step 2: Manually create the webapp directory:
You need to manually create a webapp directory in the main directory, with the structure as follows:
Step 3 jsp road strength configuration:
Add the following configuration in application.yml:
spring: mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp
Those who understand Spring mvc should be familiar with the above configuration.
Step 4: Write the jsp page:
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><html><head> <title>Title</title></head><body> <table> <c:forEach var="article" items="${list}"> <tr> <td>${article.id}</td> <td>${article.title}</td> <td>${article.summary}</td> <td>${article.createTime}</td> </tr> </c:forEach> </table></body></html>Step 5: Write the Controller:
@RequestMapping("/listJsp") public String getArticleListJsp(Model model, String title, @RequestParam(defaultValue = "10") Integer pageSize, Integer pageNum) { if (pageSize == null) { pageSize = 10; } if (pageNum == null) { pageNum = 1; } int offset = (pageNum - 1) * pageSize; List<Article> list = articleService.getArticles(title, 1L, offset, pageSize); model.addAttribute("list", list); return "articles"; }Step 6 to access the results page:
4. Summary
Overall, Spring boot is friendly to thymeleaf and Freemaker support, and the configuration is relatively simple. In actual development, most of them are mainly based on these two template engines, and there are few JSPs. JSPs may now be used more during the experiment or learning stage. The more troublesome thing about jsp configuration is that it is not like the first two. The online statement is basically the same, but there are many statements about Jsp configuration, such as whether it is necessary to change the jar package to a war package? Does the dependency of jsp need to be set to provide, etc. This mainly depends on whether you want to deploy the program to external tomcat in the end or run the jar directly? Because this article directly runs the Application class under idea, these operations are not needed.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.