Spring Boot Learning (iv) Web Development Rendering page--Thymeleaf__web

Source: Internet
Author: User
Tags html page object model

We have experienced a simple RESTful API development service;

To load static resource access

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

Default configuration

Spring boot defaults to provide a static resource directory location to be placed under Classpath, the directory name should conform to the following rules:/static/public/resources/meta-inf/resources

Example: We can create a static in the src/main/resources/directory, where a picture file is placed. After you start the program, try to access http://localhost:8080/GG.jpg. If you can display the picture, the configuration is successful.

Rendering Web pages

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

Template engine

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

Spring Boot provides the default configuration of the template engine mainly in the following ways: Thymeleaf freemarker Velocity Groovy Mustache

Spring Boot recommends using these template engines to avoid using JSPs, and if you must use JSP, you will not be able to implement many of the features of spring boot, as described later: 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 the path, 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, created by Daniel Fernández, based on the Apache License 2.0 license, and is the author of the Java Cryptographic Library Jasypt.

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

Sample Template:

01 <table>
02 <thead>
03 <tr>
Geneva       <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.