java version of spring cloud + spring boot + redis multi-tenant social e-commerce platform (seven) Web application development (1)

Static resources visit
our Web application development time, need to reference a lot of js, css, pictures and other static resources.

The default configuration
Spring Boot default resource directories provide static position to be placed in the classpath, directory name, subject to the following rules:

/ static
/ public
/ Resources
/ META-INF / Resources
Example: we can create static in src / main / resources / directory, place a picture file at that location. After starting the program, try to access http: // localhost: 8080 / D.jpg . If they can show pictures, the configuration is successful.

Web pages render
in the previous example, we are to handle requests by @RestController, so the content is returned json object. So if you need to render html pages, when, how to achieve it?

Template engine
Spring on Dynamic HTML Boot can still achieve perfectly competent, and provides default configuration supports a variety of template engine, so at the recommended template engine, we can quickly get started developing dynamic websites.

Spring Boot provides a template engine default configuration are the following:

Thymeleaf
FreeMarker
Velocity
Groovy
Mustache
Spring Boot is recommended to use these template engine, avoid using JSP, if we must use JSP will not be able to achieve a variety of features Spring Boot, specifically visible later: JSP support configuration

When you use any of the above template engine, they default template configuration path: src / main / resources / templates. Of course, you can modify this path, specifically how to modify, you can query and modify the configuration properties for each subsequent template engine.

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

Thymeleaf provides an optional module for Spring MVC integration, application development, you can use Thymeleaf to completely replace JSP or other template engines, such as Velocity, FreeMarker and so on. The main objective is to provide a Thymeleaf can be correctly displayed in the browser, a good way to create a template format, it can also be used as static modeling. You can use it to create a verified XML and HTML templates. With respect to the write logic or code, developers only need to add a label to the property to the template. The next execution, these properties will be on the label DOM (Document Object Model) pre-established logic.

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>

You can see Thymeleaf mainly in how properties are added to html tags, the browser when parsing html, when checking that there is no property when ignored, so Thymeleaf template can be opened to show directly through the browser, so that is very conducive to front and rear ends separation.

Use Thymeleaf in Spring Boot, only we need to introduce the following dependence, and to complete the preparation of the template file in the default template path src / main / resources / templates.

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

JAVASpring Cloud needs of large enterprises distributed micro cloud services built B2B2C e-commerce platform source code, please add penguin beg: 3536247259

Guess you like

Origin blog.csdn.net/qq_42748864/article/details/95162544