Text: ' One text ', ' another one! ',...
Digital text: 0, 3.0, 12.3,...
Boolean literal: True, False
NULL literal: null
Text markers: One, sometext, main,...
The use of variable expressions, as we have seen in the previous code, is the most commonly used expression in our usual development, to map the dynamic data of the background Java class to the page, for example:
Final effect:
2.2.2 Select expression *{...} The selection expression is equivalent to selecting an object that is not prefixed to the object when it is used, and uses the attribute's key to display the content directly, with the following code:
<div th:object="${goods}"> <span th:text="${goods.name}"></span> <span th:text="*{price}"></span> <span th:text="${#dates.format(goods.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span></div>
Final effect:
iMac 7999.0 2018-08-10 14:03:51
Summary: *{price} = ${goods.price} simply eliminates the "goods." Prefix, the effect is the same.
2.2.3 link expression @{...} To convert the URL, the code is as follows:
<a th:href="@{/footer(id=666,name=laowang)}">链接</a>
The result of the final rendering:
<a href="/footer?id=666&name=laowang">链接</a>
Link expressions, which can be passed as arguments, separated by commas.
Server root relative path: @{~/path/to/something}
2.2.4 Text Manipulation Text operations are divided into two: text spelling, text substitution
Text spelling:
<span th:text="'我叫'+${name}"></span>
Text substitution:
Syntax for text substitution: | content ${tag}|
<span th:text="|我叫${name},是一名开发工程师。|"></span>
2.2.53-Dollar Expression
2.2.6 Double Bracket Action <p th:text="${val}">...</p><p th:text="${{val}}">...</p>
Results:
<p>1234567890</p><p>1,234,567,890</p>
2.2.7 Embedding Text Labels While standard tags can meet almost all business scenarios, in some cases we prefer to write directly to HTML text, for example:
<p>Hello, [[${name}]]</p>
Embedded text has two ways of writing "[[...]]" and "[(...)]", respectively, like Th:text and Th:utext, for example:
<p> [[${name}]]</p><p> [(${name})]</p>
See the effect is this:
2.3 Overview of Expression objects The object in the expression can help us to deal with the content that we want to show, such as the tool class of the expression dates can format time, these built-in classes use skillfully, can let us use thymeleaf efficiency to improve a lot.
2.3.1 Expression Base Object
#ctx
: Manipulate the current context.
#vars:
The action context variable.
#request
: (for Web project only) HttpServletRequest
objects.
#response
: (for Web project only) HttpServletResponse
objects.
#session
: (for Web project only) HttpSession
objects.
#servletContext
: (for Web project only) ServletContext
objects.
2.3.2 Expression Utility Class
-
#execInfo
: The tool class that operates the template, including some template information, such as: ${#execInfo. templatename}
.
-
#uris
: tool for URL processing
-
#conversions
: Methods for executing the configured conversion service (if any).
-
#dates
: Method from java.util.Date
object for processing time, such as: Formatting.
-
#calendars
: Similar to #dates
, but from the Java.util.Calendar
object.
-
#numbers
: Used to format numbers.
-
#strings
: Methods for String
objects:contains, StartsWith, prepending/appending, etc.
-
#objects
: Normal object method.
-
#bools
: A tool that determines the type of bool.
-
#arrays
: Array manipulation tool.
-
#lists
: List operation data.
-
#sets
: Set manipulation tool.
-
#maps
: Map Operation tool.
-
#aggregates
: A tool that operates an array or collection.
Specific methods in each class, click to view: www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-b-expression-utility-objects
3.IDEA Set Thymeleaf Auto complete First on:
Idea is enabled by default Thymeleaf plug-in support, if you do not trust the need to verify, please visit: www.jetbrains.com/help/idea/2018.2/thymeleaf.html
But just configuring the above effect is still not working, because you want to declare the Thymeleaf namespace in HTML xmlns:th="http://www.thymeleaf.org"
, the complete code is as follows:
<!DOCTYPE html>
The key code is:
Xmlns:th= "Http://www.thymeleaf.org"
So when you enter "th:" In the code, you will see all the tags of Thymeleaf.
Third, Spring Boot integrated Thymeleaf3.1 development environment
- Spring Boot 2.0.4
- Thymeleaf 3.0.9
- JDK 8
- Windows 10
- Idea 2018.2
Before formally integrating the Thymeleaf engine, consider the directory structure
3.2 Spring MVC directory structure To remove the package name, let's explain what these directories mean:
- Common general public class
- Controller Controllers Class
- DAO Data Interaction Classes
- Service business logic Processing class
- Application.java Startup file
- Resources Static File storage folder
- Resources/templates all thymeleaf Directory storage directory
- Resources/application.properties Global Configuration Class
- Pom.xml Maven configuration file
3.3 Spring Boot Integrated Thymeleaf is divided into four steps:
- Pom.xml adding Thymeleaf template engine
- Application.properties Configuring Thymeleaf Information
- Create a controller class, write code
- Create templates, write HTML code
Next, we look at specific steps separately.
3.3.1 Pom.xml Add Thymeleaf template engine <!--thymeleaf模板--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
3.3.2 Application.properties Configuration Thymeleaf Information # 启用缓存:建议生产开启spring.thymeleaf.cache=false# 建议模版是否存在spring.thymeleaf.check-template-location=true# Content-Type 值spring.thymeleaf.servlet.content-type=text/html# 是否启用spring.thymeleaf.enabled=true# 模版编码spring.thymeleaf.encoding=utf-8# 应该从解析中排除的视图名称列表(用逗号分隔)spring.thymeleaf.excluded-view-names=# 模版模式spring.thymeleaf.mode=HTML5# 模版存放路径spring.thymeleaf.prefix=classpath:/templates/# 模版后缀spring.thymeleaf.suffix=.html
Thymeleaf Common Configuration Instructions
Configuration Items |
type |
Default Value |
Recommended Values |
Description |
Spring.thymeleaf.enabled |
bool |
True |
Default |
is enabled |
Spring.thymeleaf.mode |
String |
Html |
Default |
Template type, which can be set to HTML5 |
Spring.thymeleaf.cache |
bool |
True |
Default |
If caching is enabled, the build environment recommendation is set to True |
Spring.thymeleaf.prefix |
String |
classpath:/templates/ |
Default |
Template Storage Path |
Spring.thymeleaf.suffix |
String |
. html |
Default |
Template suffix |
Spring.thymeleaf.servlet.content-type |
String |
Text/html |
Default |
Content-type value |
Spring.thymeleaf.encoding |
String |
- |
Utf-8 |
Template encoding |
3.3.3 Creating a controller class, writing code We create Index.java in the Controller folder with the following code:
package com.hello.springboot.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/")public class Index { @RequestMapping("/") public ModelAndView index() { ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("name", "王磊的博客"); return modelAndView; }}
Key code Interpretation:
- @ResponseBody Note: If you use this annotation, the returned results will be output directly instead of being rendered using the template engine
- Use the Modelandview object to specify the view name & Add View Object
3.3.4 creating templates, writing HTML code We create the index.html under Resources/templates, the code is as follows:
<!DOCTYPE html>
Start debugging, in Browser input: http://localhost:8080/
The effect is as follows:
Related Code github:github.com/vipstone/springboot-example.git
Iv. references Thymeleaf Official Document Thymeleaf:www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html
Thymeleaf Official documents Spring + THYMELEAF: