(2) SpringBoot Basics - Access to Static Resources and Use of Thymeleaf Template Engine (1) SpringBoot Basics - Introduction and First Experience with HelloWorld

1. Description

In the process of application system development, it is inevitable to use static resources (the browser can understand it, it can have variables, such as HTML pages, css style files, text, property files, pictures, etc.);

And SpringBoot has a built-in Thymeleaf template engine, which can be used for rendering processing. The default version is 2.1, and the version number of Thymeleaf can be redefined. Configure the following in the maven configuration file:

<properties>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

Second, the default static resource mapping

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

By default, SpringBoot will look for the corresponding static resources from the three directories of static, public, and resources under META-INF/resources, and the template of the template engine needs to be placed in the templates directory of resources by default;

3. Examples

1. Access to static resources

  • Create a maven project, create static and templates folders in the resources directory, and place the picture success.jpg in static;
  • Create a startup class. For details, please see: (1) SpringBoot Basics - Introduction and First Experience of HelloWorld ;
  • Start the project, visit, http://localhost:8080/success.jpg, the picture can be displayed successfully on the page;

  

 

2. Thymeleaf template engine

  ①. Before using Thymeleaf, you need to introduce the dependent class library:

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

 

  ②, create a startup class Application.java;

  ③. Create the control layer HelloController.java;

package com.cn.controller;/**
 * @Description: Created by xpl on 2018-05-01 13:23.
 */

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

/**
 * Created by xpl on 2018-05-01 13:23
 **/

@RestController
public  class Hello Controller {

    @RequestMapping("/getThymeleaf")
    public ModelAndView getThymeleaf() {
        ModelAndView modelAndView = new ModelAndView("hello");
        modelAndView.addAllObjects(new HashMap<String, String>(){
            {
                this.put("name","Andy");
            }
        });
        return modelAndView;
    }

}

 

  ④. Create the Thymeleaf template hello.html, and use th: to access variables;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>
        Title</title>
</head>
<body>

<h1>Hello,</h1>
<h1 th:text="${name}"/>
</body>
</html>

  ⑤. Start the project and visit http://localhost:8080/getThymeleaf, as follows:

  

  The directory structure is as follows:

  

 

 

 

Full example: https://gitee.com/lfalex/spring-boot-example

 Copyright statement: This article is an original article by the blogger, please indicate the source for reprinting, thank you!

 

Guess you like

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