Although a lot of development now, have adopted a completely separate front and back end model, that is, the back end only provides the data interface, the front end through AJAX requests to obtain data, do not need to use a template engine. The advantage of this approach is that the front and back ends are completely separated, and with the improvement of front-end engineering tools and MVC framework in recent years, the maintenance cost of this mode is relatively lower. However, this pattern is not conducive to SEO and may be slightly worse in terms of performance, and there are some scenarios where using a template engine is more convenient, such as mail templates. This article focuses on the integration of Spring Boot with template engines Thymeleaf, Freemaker, and JSP.
First, integrate Thymeleaf
Step 1: Import jar package (thymeleaf counterpart starter) :
1
2
3
4
<``dependency``>
<``groupId``>org.springframework.boot</``groupId``>
<``artifactId``>spring-boot-starter-thymeleaf</``artifactId``>
</``dependency``>
Step 2: Configure thymeleaf:
1
2
3
4
5
6
7
8
9
spring:
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
cache: false
suffix: .html
encoding: UTF-8
content-type: text/html
mode: HTML5
Prefix: specifies the directory where the template resides
Check-tempate-location: checks whether the template path exists
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.
Encoding&content-type: this should be familiar to you, and works the same way as setting output properties in servlets.
Mode: Please refer to the instructions on the official website, and this is 2.x. Unlike 3.0, this article automatically introduces 2.15.
Step 3 Write the Thymeleaf template file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<! 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>
The < th > number < / th >
The < th > title < / th >
The < th > abstract < / th >
<th> Create time </th>
</tr>
</thead>
<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 the biggest feature is that the tag as HTML element attributes exist, that is, the page can be directly through the browser to preview, but there is no data, this is very convenient for debugging.
Step 4 Configure Controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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 is @Controller, not @RestController, because @RestController automatically returns the result as a string.
Step 5 View the results
Spring Boot and Freemarker integration
1. Introduce jar packages (Freemarker’s starter)
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2. Configure freemarker:
1
2
3
4
5
6
7
8
spring:
freemarker:
template-loader-path: classpath:/templates/
suffix: .ftl
content-type: text/html
charset: UTF-``8
settings:
number_format:
'0.##'
Other than Settings, the configuration options are similar to Thymeleaf. Settings affect freemarker certain behavior, such as the date format, digital format, etc., interested students can refer to website provides instructions: freemarker.apache.org/docs/api/fr…
3. Write a Freemarker template file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<html>
<title> List of articles </title>
<body>
<h6>Freemarker template engine </h6>
<table border=``"1"``>
<thead>
<tr>
The < th > number < / th >
The < th > title < / th >
The < th > abstract < / th >
<th> Create 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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 page:
Three, Sring Boot and JSP integration:
JSP templates are rarely used in formal project development these days, so Spring Boot doesn’t support JSP very well, so configuration is a bit more complicated than Thymeleaf and Freemaker.
Step 1 Import the JAR package:
1
2
3
4
5
6
7
8
<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 dependency is used to support JSPS. Note that if you are running in an external Tomcat, you need to set scope to provide to prevent JAR package conflicts.
Step 2 Create webApp directory manually:
You need to manually create a webApp directory in the main directory. The structure is as follows:
Step 3 JSP configuration:
Add the following configuration to application.yml:
1
2
3
4
5
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
The above configuration should be familiar to those who know Spring MVC.
Step 4 write a JSP page:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ 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 border=``"1"``>
<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 Controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
@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 Visit the results page:
Four,
In general, Spring Boot is friendly to Thymeleaf and Freemaker support, and the configuration is relatively simple. In actual development, these two template engines are mostly used, and JSP is rarely used, which is probably more in the experimental or learning stage. JSP configuration more troublesome things is not like the former two, the online statement is basically the same, but there are many kinds of JSP configuration, for example, is it necessary to change jar package into war package? Do JSP dependencies need to be set to provide, etc., depending on whether you eventually deploy the application to external Tomcat or run the JAR directly? Since this article runs the Application class directly under IDEA, these operations are not required.
5. Materials recommendation
Although spring recruitment has ended, but fans do not be disappointed, I find huawei friends to sort out an internal data “6th edition: Internet Factory interview questions” and classify 4 PDF, a total of 926 pages! , I hope you next spring recruit, gold nine silver eleven refueling
The entire information package, Includes Spring and Spring Boot/Cloud, Dubbo, JVM, collection, multithreading, JPA, MyBatis, MySQL, big data, Nginx, Git, Docker, GitHub, Servlet, JavaWeb, IDEA, Redis, algorithm, interview questions and so on almost cover Java Basic and alibaba and other big factory interview questions, such as technology stack!
It is said that some friends have successfully joined ant Financial, Bytedance and other big companies through this information.
Moreover, these materials are not scanned version, the text inside can be directly copied, very convenient for us to learn: