Thymeleaf objects, define variables, URL parameters, and label custom attributes _thymeleaf

Source: Internet
Author: User
Tags numeric value string format

As stated in the title, this article focuses on the built-in objects in Thymeleaf (list parsing, date formatting, number formatting, and so on), defining variables, getting URL parameters, and customizing attributes in page labels.

If you are not clear about the basic use of thymeleaf, maven dependencies, and so on, you can first read my other article, "Preliminary use of Thymeleaf". Controller part

@Controller public
class Indexcontroller {

    @GetMapping (value = ' index ') public
    String Index (model model, HttpServletRequest request) {
        list<string> datas = new arraylist<string> ();
        Datas.add ("Knowledge Forest");
        Datas.add ("http://www.zslin.com");
        Datas.add ("393156105");
        Model.addattribute ("Datas", datas);
        Model.addattribute ("Curdate", New Date ());

        Model.addattribute ("Money", Math.random () *100);
        Return "index";
    }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

In the model of this controller there are several data: A String type list, a Date object and a numeric value, these things in the actual application development process is very extensive, the following specific look at the thymeleaf in how to parse the data. Date formatting

<span th:text= "${#dates. Format (curdate, ' Yyyy-mm-dd HH:mm:ss ')}" ></span>
1 1

Note: You can format dates by using the Format function of the built-in object dates, in the Format function, the first parameter is the Date object, and the 22 arguments are date formats (the rule is the same as SimpleDateFormat)

It is to be noted that:

· Built-in objects generally end with s, such as dates, lists, numbers, etc.

· You need to add a # number before you can use a built-in object in front of the object name. Number formatting

<span th:text= "${#numbers. Formatdecimal (Money, 0, 2)}" ></span>
1 1

Note: This example indicates that two-bit decimal places are reserved and integer digits are automatically;

<span th:text= "${#numbers. Formatdecimal (Money, 3, 2)}" ></span>
1 1

Description: This example indicates that the length of the list is preserved with two-bit decimal places, 3-bit integer digits (not enough before plus 0)

<span th:text= "${#lists. Size (datas)}" ></span>
1 1

Description: Use #lists.size to get the length of the list. Get URL parameter value

<span th:text= "${#httpServletRequest. GetParameter (' page ')} ' ></span>
1 1

Note: When you visit http://localhost:1105/index?page=5, the page will get the value of the pages: 5. Defining variables

<div th:with= "curpage=${#httpServletRequest. GetParameter (' page ')} ' >
    
1 1

Description: View the source code on the page can see: <span mymoney= "91.6059494319957" mydate= "2016-31-02" ></SPAN>, description custom attributes used: Th:attr, multiple properties with Separated. Built-in objects

This simple description of the more commonly used dates, lists, numbers these built-in objects, in Thymeleaf there are many built-in objects, such as strings is also very common, use the same as the use of the Java.lang.String class.

The built-in objects in Thymeleaf are:

#dates: Date format built-in objects, the specific method can refer to java.util.Date;

#calendars: Similar to #dates, but is the Java.util.Calendar class method;

#numbers: digital format;

#strings: String format, specific methods can refer to java.lang.String, such as StartsWith, contains, etc.;

#objects: refer to Java.lang.Object;

#bools: A Boolean-type tool is judged;

#arrays: Tools for array manipulation;

#lists: The tool of List operation, refer to java.util.List;

#sets: Set operation Tool, refer to Java.util.Set;

#maps: Map operation Tool, refer to Java.util.Map;

#aggregates: A tool for manipulating arrays or collections;

#messages: A tool for manipulating messages.

Sample code: HTTPS://GITHUB.COM/ZSL131/THYMELEAF-STUDY/TREE/MASTER/STUDY05

This article is from the "Knowledge Forest"

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.