Spring Cloud Spring Boot mybatis Distributed Microservice Cloud Architecture (7) Development of Web Applications (1)

Static resource access

When we develop web applications, we need to reference a large number of static resources such as js, css, and pictures.

default allocation

By default, Spring Boot provides a static resource directory location that needs to be placed on the classpath, and the directory name must meet the following rules:

  • /static
  • /public
  • /resources
  • /META-INF/resources

Example: We can src/main/resources/create a directory and staticplace an image file in this location. After launching the program, try to access http://localhost:8080/D.jpg. If the picture can be displayed, the configuration is successful.

render web pages

In the previous examples, we handled requests through @RestController, so the returned content is a json object. So if you need to render an html page, how to do it?

template engine

Spring Boot is still perfectly competent in the implementation of dynamic HTML, and provides default configuration support for a variety of template engines, so under the recommended template engine, we can quickly start developing dynamic websites.

The main types of template engines that Spring Boot provides by default are as follows:

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

Spring Boot recommends using these template engines and avoiding the use of JSP. If you must use JSP, you will not be able to implement various features of Spring Boot. For details, see the following: Supporting JSP Configuration

When you use any of the above template engines, their default template configuration path is: src/main/resources/templates. Of course, this path can also be modified. How to modify it can be queried and modified in the configuration properties of subsequent template engines.

Thymeleaf

Thymeleaf is an 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, licensed under the Apache License 2.0, created by Daniel Fernández, the author of the Java encryption library Jasypt.

Thymeleaf provides an optional module for integrating Spring MVC. In application development, you can use Thymeleaf to completely replace JSP or other template engines, such as Velocity, FreeMarker, etc. The main goal of Thymeleaf is to provide a way to create a well-formed template that is displayed correctly by the browser, and thus can also be used for static modeling. You can use it to create validated XML and HTML templates. Rather than writing logic or code, developers can simply add tag attributes to the template. These tag attributes then execute pre-defined logic on the DOM (Document Object Model).

Example 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>

It can be seen that Thymeleaf is mainly added to the html tag in the form of attributes. When the browser parses the html, it will ignore the attributes that are not found, so the Thymeleaf template can be directly opened and displayed through the browser, which is very beneficial to the front and back ends. separation.

To use Thymeleaf in Spring Boot, you only need to introduce the following dependencies and src/main/resources/templateswrite a template file in the default template path.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

source code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325363842&siteId=291194637