Basic syntax: th namespace
Basic Syntax: Expression Syntax
Modify label text value
Code example:
<p th:text="标签体新值">标签体原始值</p>
th:text role
- Open the HTML file directly with the browser without parsing by the server, and what you see is "the original value of the tag body"
- After server parsing, the Thymeleaf engine replaces the " original value of the tag body" according to the "new value of the tag body" specified by the th:text attribute
Literal
"Literal quantity" is a concept that is often encountered, and we can understand its meaning by comparing it with "variables".
// a是变量,100是字面量
int a = 100;
System.out.println("a = " + a);
- Variable: The variable name string itself is not its value, what it points to is its value
- Literal quantity: it is the literal meaning, what we see directly from the "literal" is its value
Now what we use in the th:text attribute is "literal value", which does not refer to any other value .
Modify the specified attribute value
Code example:
<input type="text" name="username" th:value="文本框新值" value="文本框旧值" />
Parse the URL address
basic grammar
Code example:
<p th:text="@{/aaa/bbb/ccc}">标签体原始值</p>
After parsing, we get:
/view/aaa/bbb/ccc
So the function of @{} is to append "context path" before the string
The advantage of this syntax is that during the actual development process, when the project is deployed in different environments, the name of the web application may change. So the context path cannot be hardcoded. After dynamically obtaining the context path through @{}, no matter how you change it, you are not afraid!
Home page using URL address resolution
If we directly access index.html itself, then index.html does not need to go through Servlet,
Of course it doesn't go through the templating engine, so any Thymeleaf expressions on index.html won't be parsed.
Solution:
Access index.html through Servlet, so that the template engine can render the page:
Further benefits:
Through the above example, we can see that all requests related to business functions can ensure that they are processed through Servlet, which makes it convenient for us to uniformly limit these requests to specific rules.
Append request parameters to the URL address
Referring to the official documentation:
execute the expression directly
Servlet code:
request.setAttribute("reqAttrName", "<span>hello-value</span>");
Page code:
<p>有转义效果:[[${reqAttrName}]]</p>
<p>无转义效果:[(${reqAttrName})]</p>
Execution effect:
<p>有转义效果:<span>hello-value</span></p>
<p>无转义效果:<span>hello-value</span></p>
Basic Syntax: Accessing Domain Objects
domain object
request domain
In the scenario of request forwarding, we can use the storage space provided by the HttpServletRequest object to help us carry data and send the data to the forwarded target resource.
Request domain: the storage space provided to us inside the HttpServletRequest object
session domain
application domain
PS: When the view we use is JSP, there are 4 domain objects
pageContext
request: request domain
session: session domain
application: application domain
So in the context of JSP use, we can say that there are 4 domain objects, and now Thymeleaf is used without pageContext.
Store data in attribute fields in Servlet
Operation Request Field
Code in Servlet:
String requestAttrName = "helloRequestAttr";
String requestAttrValue = "helloRequestAttr-VALUE";
request.setAttribute(requestAttrName, requestAttrValue);
Thymeleaf expressions:
<p th:text="${helloRequestAttr}">request field value</p>
Operation session domain
Code in Servlet:
// ①通过request对象获取session对象
HttpSession session = request.getSession();
// ②存入数据
session.setAttribute("helloSessionAttr", "helloSessionAttr-VALUE");
Thymeleaf expressions:
<p th:text="${session.helloSessionAttr}">这里显示会话域数据</p>
Operational application domain
Code in Servlet:
// ①通过调用父类的方法获取ServletContext对象
ServletContext servletContext = getServletContext();
// ②存入数据
servletContext.setAttribute("helloAppAttr", "helloAppAttr-VALUE");
Thymeleaf expressions
<p th:text="${application.helloAppAttr}">这里显示应用域数据</p>
Basic Syntax: Get Request Parameters
Specifically, what we are talking about here is getting request parameters on the page (template page). The underlying mechanism is:
one name one value
Page code:
<p th:text="${param.username}">这里替换为请求参数的值</p>
Page display effect:
one name multiple values
Page code:
<p th:text="${param.team}">这里替换为请求参数的值</p>
Page display effect:
If you want to get a certain value precisely, you can use array subscripts. Page code:
<p th:text="${param.team[0]}">这里替换为请求参数的值</p>
<p th:text="${param.team[1]}">这里替换为请求参数的值</p>
Page display effect:
Section 8 Basic Syntax: Built-in Objects
concept
The so-called built-in objects are actually objects that can be used directly in expressions .
Basic built-in objects
Usage example:
<h3>表达式的基本内置对象</h3>
<p th:text="${#request.getClass().getName()}">这里显示#request对象的全类名</p>
<p th:text="${#request.getContextPath()}">调用#request对象的getContextPath()方法</p>
<p th:text="${#request.getAttribute('helloRequestAttr')}">调用#request对象的getAttribute()方法,读取属性域</p>
The basic idea:
- If you don't know what methods this object has available, then get the full class name through getClass().getName(), and then go back to the Java environment to see what methods this object has
- Methods of built-in objects can be called directly
- If you need to pass parameters when calling a method, you can also pass in parameters directly.
public built-in object
In the Servlet, the List collection data is stored in the request field:
request.setAttribute("aNotEmptyList", Arrays.asList("aaa","bbb","ccc"));
request.setAttribute("anEmptyList", new ArrayList<>());
Page code:
<p>#list对象isEmpty方法判断集合整体是否为空aNotEmptyList:<span th:text="${#lists.isEmpty(aNotEmptyList)}">测试#lists</span></p>
<p>#list对象isEmpty方法判断集合整体是否为空anEmptyList:<span th:text="${#lists.isEmpty(anEmptyList)}">测试#lists</span></p>
The source code location corresponding to the public built-in object: