SpringBoot - web development template engine introduced Thymeleaf

  1, the introduction of Thymeleaf template engine

  org.springframework.boot

  spring-boot-starter-thymeleaf

  2.2.1.RELEASE

  2, configuration properties

  In fact, it can be used directly, without configuration. But Spring Boot official document recommended to prevent buffering during development, you would add the following line in application.properties file on the line:

  spring.thymeleaf.cache=false

  3, Thymeleaf use

  Source document

  package org.springframework.boot.autoconfigure.thymeleaf;

  import java.nio.charset.Charset;

  import java.nio.charset.StandardCharsets;

  import java.util.List;

  import org.springframework.boot.context.properties.ConfigurationProperties;

  import org.springframework.http.MediaType;

  import org.springframework.util.MimeType;

  import org.springframework.util.unit.DataSize;

  @ConfigurationProperties(prefix = "spring.thymeleaf")

  public class ThymeleafProperties {

  private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

  public static final String DEFAULT_PREFIX = "classpath:/templates/";

  public static final String DEFAULT_SUFFIX = ".html";

  /**

  * Whether to check that the template exists before rendering it.

  */

  private boolean checkTemplate = true;

  /**

  * Whether to check that the templates location exists.

  */

  private boolean checkTemplateLocation = true;

  /**

  * Prefix that gets prepended to view names when building a URL.

  */

  private String prefix = DEFAULT_PREFIX;

  /**

  * Suffix that gets appended to view names when building a URL.

  */

  private String suffix = DEFAULT_SUFFIX;

  / ** Zhengzhou flow of the hospital http://mobile.zzzzyy120.com/

  * Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.

  */

  private String mode = "HTML";

  /**

  * Template files encoding.

  */

  private Charset encoding = DEFAULT_ENCODING;

  /**

  * Whether to enable template caching.

  */

  private boolean cache = true;

  }

  }

  As long as the HTML page on the classpath: / templates /, Thymeleaf will automatically render:

  use

  1, introduced thymeleaf namespace ()

  Hello static resource template engine configuration successfully

  2, the preparation of the Controller class

  package com.hern.controller;

  import org.springframework.web.bind.annotation.RequestMapping;

  import org.springframework.web.bind.annotation.RestController;

  /**

  * @program: springboot-web-study

  * @Description: Test Environment Configuration

  * @author: 

  * @create: 2019-12-02 16:51

  **/

  @Controller

  public class HelloController {

  @RequestMapping(value = "hello")

  public String hello(){

  return "hello";

  }

  }

  3, enter localhost in the browser address bar: 8080 / hello test

Guess you like

Origin www.cnblogs.com/gnz49/p/11971722.html