Spring Boot Learning (d) Web Development rendering page--Thymeleaf__web

Source: Internet
Author: User
Tags object model

We have experienced a simple RESTful API development service;

To load static resource access

When we develop Web applications, we need to cite a large number of JS, CSS, pictures and other static resources.

Default configuration

Spring boot defaults to provide static resource directory locations that need to be placed under Classpath, and directory names should conform to the following rules:/static/public/resources/meta-inf/resources

For example: We can create static in the src/main/resources/directory and place a picture file at that location. After you start the program, try to access http://localhost:8080/GG.jpg. If the picture can be displayed, the configuration is successful.

Rendering Web pages

In the previous example, we all handled the request through @restcontroller, so the returned content was a JSON object. So if you need to render HTML pages, how do you implement them?

Template engine

Spring boot is still perfect for dynamic HTML implementations and provides the default configuration support for a variety of template engines, so we can quickly start developing dynamic Web sites under the recommended template engine.

Spring Boot provides the default configuration of the template engine mainly as follows: Thymeleaf freemarker Velocity Groovy Mustache

Spring Boot recommends using these template engines to avoid using JSPs, and it will not be possible to implement the many features of Spring boot, as shown in the following article: Support for JSP configuration

When you use any of the above template engines, their default template configuration path is: Src/main/resources/templates. Of course, you can modify this path, specifically how to modify, in the subsequent template engine configuration properties to query and modify.

Thymeleaf

Thymeleaf is a XML/XHTML/HTML5 template engine that can be used for application development in Web and non-web environments. It is an open source Java library, based on the Apache License 2.0 license, created by Daniel Fernández, author of the Java Cryptographic Library Jasypt.

Thymeleaf provides an optional module for integrating Spring MVC, where you can use Thymeleaf to completely replace a JSP or other template engine, such as velocity, freemarker, and so on. The primary goal of Thymeleaf is to provide a well-formed template creation method that can be properly displayed by the browser, and therefore can also be used as a static model. You can use it to create validated XML and HTML templates. Instead of writing logic or code, developers simply add tag attributes to the template. Next, these tag properties perform predefined logic on the DOM (Document Object model).

Sample Template:

01 <table>
02 <thead>
03 <tr>
      <th th:text= #{msgs.headers . Name} '

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.