Springboot Introduction to the use of thymeleaf one

Source: Internet
Author: User

Using Cshtml,razor in a framework that supports Razor, such as MVC3 or later versions of. NET, is a simple programming syntax for embedding server-side code in Web pages. There is also a view engine similar to. NET when using Springboot to develop MVC.
Spring Boot provides a number of template engines, including the freemarker,groovy,thymeleaf,velocity and mustache,spring boot recommendations using the Thymeleaf as the template engine, Because Thymeleaf provides the perfect support for spring MVC. Thymeleaf is a Java class library, which is a XML/XHTML/HTML5 template engine that can be used as the view layer for MVC Web applications. Thymeleaf also provides additional modules to integrate with spring MVC, so we can use Thymeleaf to completely replace the JSP.
First, thymeleaf configuration
What are the Thymeleaf properties that can be configured? We can find properties under Org.springframework.boot.autoconfigure.thymeleaf Thymeleafproperties.class, Springboot convention is greater than configuration, so in Thymeleafpropert Ies.class also have default values, if we want to change the default value can be set in Application.properties. Here is the default value for it. The default path under Templates, the file is an HTML file.

Second, the introduction of the project Thymeleaf

This is also done on the basis of the example of the previous Springboot blog, Here the need to introduce thymeleaf in Pom.xml, here to note, because the use of SPRING5, if the introduction of Thymeleaf version is incorrect can be an error, and different spring introduced thymeleaf of Artifactid is not the same.

        <Dependency>            <groupId>Org.thymeleaf</groupId>            <Artifactid>Thymeleaf-spring5</Artifactid>            <version>3.0.9.RELEASE</version>        </Dependency>

Also, if you are using HTML pages, add a line to the HTML

<xmlns:th= "http://www.thymeleaf.org">

Third, testing

Here a controller and a Html.thymeleaf property are created with the default property values, and if necessary change can be made in resources/ Application.properties changed, the prefix name is spring.thymeleaf.

 PackageCom.example.demo;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;//@RestController@Controller Public classHellocontroller {@RequestMapping (value= "/hello", method =requestmethod.get) PublicString Hello (model model) {Model.addattribute ("Name", "Cuiyw"); return"Hello"; }}
<!DOCTYPE HTML><HTMLxmlns:th= "http://www.thymeleaf.org"><Head>    <title>Hello</title>    <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /></Head><Body><PTh:text= "' hello! , ' + ${name} + '! ' ></P></Body></HTML>

Springboot Introduction to the use of thymeleaf one

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.