let's start by constructing a form to explain the use of thymeleaf. for illustrative purposes, take the classic registration as an example.
This is the form of thymeleaf,
<form action= "#" th:action= "@{/register}" th:object= "${person}" method= "POST" > </form>
action= "#" is a fixed part because the action is indicated by Th:action. The @ symbol is how thymeleaf is expressed to the URL. This is the first knowledge point in this article.
The first is the absolute path,
<a th:href= "@{http://www.baidu.com}" >
But the most common must be relative paths. Many friends confuse context-relative paths with server-relative paths, but they are different. The context-relative path starts with/begins, for example, one of your application MyApp is deployed under Tomcat, and you can access it with Http://localhost:8080/myapp, where MyApp is the context name. Then
<a th:href= "@{/login}" >
After parsing,
<a href= "/myapp/login" >
The server-relative path and its difference is that the server-relative path does not assume that your resources are within the application context, (you may deploy multiple apps), which allows you to access other contexts on the same server. Like what
<a th:href= "@{~/other-app/hello.html}" >
After parsing,
<a href= "/other-app/showdetails.htm" >
Of course there is a relative path (relative to the Protocol), which is actually an absolute path,
<a th:href= "@{<span style=" font-family:arial, Helvetica, Sans-serif; " >//code.jquery.com/jquery-1.10.2.js</span><span style= "font-family:arial, Helvetica, Sans-serif;" } "></span>
After parsing,
<a href= "//code.jquery.com/jquery-1.10.2.js" >
Next is the URL of the Add parameter question,
<a th:href= "@{/order/details (id=3)}" >
After parsing,
<a href= "/order/details?id=3" >
Multiple parameters can be separated by commas within ().
The following forms are also supported, please understand carefully,
<a th:href= "@{/order/{id}/details (id=3,action= ' Show_all ')}" >
After parsing,
<a href= "/order/3/details?action=show_all" >
There is also a concept called URL fragment, what is URL fragment?
The above two graphs basically explain what the URL fragment is.
<a th:href= "@{/home#all_info (action= ' Show ')}" >
After parsing,
<a href= "/home?action=show#all_info" >
The next point of knowledge is Th:object,
This property is common in thymeleaf, but form will force you to write this. To integrate spring, it th:object the following in the form:
- Must be a variable expression (${...}) that represents the name of the model and cannot be navigated to the properties of the model, that is, ${a} is legitimate, but ${a.b} is illegal
- There can be no other th:object in the form, i.e. HTML forms cannot be nested
as you might guess, this object is passing data to the background. in StackOverflow There is a question, "send datas from the HTML to controller in Thymeleaf?", the answer is:Controller:
@RequestMapping (value = "/processform", method=requestmethod.post) public String processform (@ModelAttribute (value= " Foo ") foo foo) { ...}
Html:
<form action= "#" th:action= "@{/processform}" th:object= "${foo}" method= "POST" > <input type= "text" th: Field= "*{bar}"/> <input type= "Submit"/></form>
Foo.java
public class Foo { private String bar; Public String Getbar () { return bar; } public void Setbar (String bar) { this.bar = bar; }}
After reading it, you will surely be able to make it.
There is also a knowledge point is Th:field.
This attribute is important in SPRING-MVC, which is responsible for binding background bean properties, which is similar to the path in the JSP tag. There are some differences between the different types of Input,th:field. We'll talk about this later. to keep in mind, Th:field must be the selection expression (*{...}).
Finally, you may have questions about the expressions mentioned earlier. variable expression ${...} is an OGNL expression. If you use Springstandard, it is the spring expression (spel). give me an example and you'll see.
<span th:text= "${book.author.name}" >
Select Expression *{...} is much like a variable expression, except that it executes the previously selected object.
<div th:object= "${book}" > ... <span th:text= "*{title}" >...</span> ...</div>
The book is selected in the front, followed by its evaluation.
There is also an expression that does not appear before. Internationalized Expressions #{...}, as the name implies, deals with internationalization.
<table> ... <th th:text= "#{header.address.city}" >...</th> <th th:text= "#{header.address.country}" ... </th> ...</table>
International resources are typically defined in the. properties file.
Spring Mvc:java Template engine Thymeleaf (iii)