Spring Mvc:java Template engine Thymeleaf (iii)

Source: Internet
Author: User

Let's start by constructing a form to explain the use of thymeleaf. For demonstration convenience, or 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 the way the thymeleaf is expressed to the URL. This is the first knowledge point of this article.


URL expression

The first is the absolute path,

<a th:href= "@{http://www.baidu.com}" >

But the most common must be the relative path. Many friends confuse context-relative paths with server-relative paths, but they are different. Context-relative paths are all started with/, such as one of your applications MyApp deployed under Tomcat, you can access it with Http://localhost:8080/myapp, where MyApp is the context name. Then

<a th:href= "@{/login}" >
After parsing is,

<a href= "/myapp/login" >

The server-relative path and the difference is that the server-relative path does not assume that your resources are within the application context (you may deploy multiple applications), that is, it allows you to access other contexts on the same server. Like what

<a th:href= "@{~/other-app/hello.html}" >

After parsing is,

<a href= "/other-app/showdetails.htm" >

There is, of course, 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 is,

<a href= "//code.jquery.com/jquery-1.10.2.js" >


Next is the URL's add parameter problem,

<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 form is 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 the URL fragment?




The above two diagrams 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,

Th:object

This property is common in thymeleaf, but form will force you to write this. To integrate spring, it sets the th:object in the form as follows:

Must be a variable expression (${...}) that represents the name of the model and cannot be navigated to by the model's properties, that is, ${a} is legitimate, but ${a.b} cannot have other th:object within the form, that is, HTML forms cannot be nested you might guess, This object is to pass data to the background. In StackOverflow there is a question, "Send datas from HTML to controller in Thymeleaf", the answer is this: 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, you must be able to see the suddenly.
There is also a knowledge point is Th:field.
Th:field This attribute is important in SPRING-MVC and bears the burden of binding back-end bean properties, which is similar to the path in the JSP tag. There are some differences in different types of Input,th:field. Let's talk about it later. Keep in mind that Th:field must be a Select expression (*{...}).
Finally, you may have questions about the expressions mentioned earlier. Variable expression ${...} is the OGNL expression. If you use Springstandard, you are the spring expression (spel). Give an example and you'll see.
<span th:text= "${book.author.name}" >

Select an expression *{...} It's much like a variable expression, except that it performs the object that was previously selected.
<div th:object= "${book}" > ...
  <span th:text= "*{title}" >...</span> ...
</div>
The book is selected before, and then evaluated by it.
There is also an expression that does not appear in front of it. Internationalization expression #{...}, as the name implies is to deal with internationalization.
<table> ...
  <th th:text= "#{header.address.city}" >...</th>
  <th th:text= "#{header.address.country}" .... </th> ...
</table>

International resources are generally defined in the. properties file.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.