Springboot2.x--thymeleaf engine Templates

Source: Internet
Author: User

Springboot2.x--thymeleaf engine Templates

Java engine templates mainly include: Thymeleaf, Freemarker, volecity and so on, interested can go to understand the other two templates, here only say thymeleaf. (The advantages and disadvantages of the three: 76407612)

  What is Thymeleaf ?

    • 1.Thymeleaf can be run in both a network and a network-free environment, which allows the artist to view the static effect of the page in the browser, or to allow programmers to view dynamic page effects with data on the server. This is because it supports HTML prototypes, and then adds additional attributes to the HTML tag to show how the template + data is presented. When the browser interprets HTML, the undefined label attribute is ignored, so the thymeleaf template can be run statically, and when data is returned to the page, the thymeleaf tag dynamically replaces the static content, making the page dynamic.

    • 2.Thymeleaf out-of-the-box features. It provides standard and spring standard dialect, can directly apply the template to achieve jstl, OGNL expression effect, avoid the daily set of templates, the Jstl, change the label of the puzzle. Developers can also expand and create custom dialects.

    • The 3.THYMELEAF provides a spring standard dialect and an optional module that integrates seamlessly with the SPRINGMVC, enabling quick implementation of form bindings, attribute editors, and internationalization capabilities.

  Why is springboot recommended to use Thymeleaf?

    • Provides the perfect SPRINGMVC support
    • Thymeleaf is both a prototype and a page, development faster, in line with the Springboot concept.

Pom.xml introduces Thymeleaf dependency:

  

nekohtml dependency: non-strict HTML strict

Create a Application.properties file (here, for easy reading, the actual development uses the Yml file)  

  YML syntax needs to be noted: formatting needs to be aligned, do not use the TAB key!!!!

Spring:  thymeleaf:    cache:false # Turn off caching on development, otherwise you won't be able to see live pages    MODE:LEGACYHTML5 # with non-strict HTML    Encoding:utf-8    servlet:      content-type:text/htmlserver:  port:9090 #更改tomcat端口

A simple test case: Create a JavaBean and a corresponding controller

User.java

1  Packagecom.baiye.springboothello.entity;2 3  Public classUser {4     PrivateLong ID;5     PrivateString UserName;6     Private intAge ;7 8 9      PublicUser () {Ten  One     } A  -      PublicUser (Long ID, String userName,intAge ) { -          This. ID =ID; the          This. UserName =UserName; -          This. Age =Age ; -     } -  +      PublicLong getId () { -         returnID; +     } A  at      Public voidsetId (Long id) { -          This. ID =ID; -     } -  -      PublicString GetUserName () { -         returnUserName; in     } -  to      Public voidsetusername (String userName) { +          This. UserName =UserName; -     } the  *      Public intGetage () { $         returnAge ;Panax Notoginseng     } -  the      Public voidSetage (intAge ) { +          This. Age =Age ; A     } the}

Usercontroller.java

1  PackageCom.baiye.springboothello.controller;2 3 ImportCom.baiye.springboothello.entity.User;4 ImportOrg.springframework.stereotype.Controller;5 ImportOrg.springframework.ui.Model;6 Importorg.springframework.web.bind.annotation.RequestMapping;7 ImportOrg.springframework.web.bind.annotation.RequestMethod;8 9 Importjava.util.ArrayList;Ten Importjava.util.List; One  A @Controller -  Public classUsercontroller { -@RequestMapping (value = "/getuserinfo", method =requestmethod.get) the      PublicString GetUserInfo (model model) { -User User =NewUser (100L, "admin", 18); -User user2 =NewUser (101L, "John Doe", 19); -User User3 =NewUser (102L, "Zhang San", 20); +User User4 =NewUser (103L, "Harry", 21); -list<user> list =NewArraylist<>(); + List.add (user2); A List.add (USER3); at List.add (user4); -Model.addattribute ("User", user); -Model.addattribute ("list", list); -         return"UserInfo"; -     } -}

For the front-end files: HTML, CSS, JS and other static files, Springboot recommended to be stored in the resources directory of static, but here we use the Thymeleaf engine template, So HTML should be placed under the resources in another directory under--template:

Userinfo.html

1 <!DOCTYPE html SYSTEM "HTTP://WWW.THYMELEAF.ORG/DTD/XHTML1-STRICT-THYMELEAF-SPRING4-4.DTD">2 <HTMLxmlns= "http://www.w3.org/1999/xhtml"xmlns:th= "http://www.thymeleaf.org">3 <Head>4     <MetaCharSet= "UTF-8">5     <title>Hello Thymeleaf</title>6 </Head>7 <Body>8 <Div>9     <span>Access Model:</span><spanTh:text= "${user.username}"></span>Ten </Div> One <Div> A     <span>Access list</span> -     <Table> -         <thead> the         <TR> -             <th>Number</th> -             <th>Name</th> -             <th>Age</th> +         </TR> -         </thead> +         <tbody> A         <TRTh:each= "Item: ${list}"> at             <TDTh:text= "${item.id}"></TD> -             <TDTh:text= "${item.username}"></TD> -             <TDTh:text= "${item.age}"></TD> -         </TR> -         </tbody> -     </Table> in </Div> - </Body> to </HTML>

all engine templates need to be introduced, Themeleaf no exception, please introduce in the head :

Themeleaf More label Learning: 75614906

Start the main function, note the port changes, and run the following results:

Springboot2.x--thymeleaf engine Templates

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.