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, and the directory name must 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/D.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 how do you do it if you need to render an HTML page?
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:
<table> <thead> <tr> <th th:text="#{msgs.headers.name}">Name</td> <th th:text="#{msgs.headers.price}">Price</td> </tr> </thead> <tbody> <tr th:each="prod : ${allProducts}"> <td th:text="${prod.name}">Oranges</td> <td th:text="${#numbers.formatDecimal(prod.price,1,2)}">0.99</td> </tr> </tbody> </table>
You can see that thymeleaf is mainly attributed to the HTML tag, the browser when parsing the HTML, when the detection of the attributes are ignored, so the thymeleaf template can be opened directly through the browser display, which is very conducive to the separation of the front and back.
To use Thymeleaf in spring boot, simply introduce the following dependencies and write the template file under the default template path Src/main/resources/templates.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
Spring Cloud Spring Boot mybatis distributed microservices Cloud architecture-Developing Web applications