First, describe
In the application system development process, the inevitable need to use static resources (browser to understand, he can have variables, such as: HTML pages, CSS style files, text, properties files, images, etc.);
and Springboot built-in thymeleaf template engine, you can use the template engine for rendering processing, the default version is 2.1, you can redefine the version number of the Thymeleaf, in the MAVEN configuration file to configure the following:
<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
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
Springboot default will find the corresponding static resources from the static, public, and resources three directories under the Meta-inf/resources, and the template engine template needs to be placed in the Resources templates directory by default;
Iii. examples
1. Access to static resources
- Create a MAVEN project, create a static, Templates folder under the Resources directory, and place the picture success.jpg in static;
- To create the startup class, see: (a) Springboot basic-Introduction and HelloWorld experience;
- Start the project, Access, http://localhost:8080/success.jpg, pictures can be displayed on the page success;
2. Thymeleaf Template engine
①, before using thymeleaf, you need to introduce a dependent class library:
<!--using thymeleaf templates--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
②, create startup class Application.java;
③, create control layer Hellocontroller.java;
PackageCom.cn.controller;/*** @Description: Created by xPL on 2018-05-01 13:23.*/Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportOrg.springframework.web.servlet.ModelAndView;ImportJava.util.HashMap;/*** Created by XPL on 2018-05-01 13:23 **/@RestController Public classHellocontroller {@RequestMapping ("/getthymeleaf") PublicModelandview getthymeleaf () {Modelandview Modelandview=NewModelandview ("Hello"); Modelandview.addallobjects (NewHashmap<string, string>(){ { This. Put ("name", "Andy"); } }); returnModelandview; }}
④, create Thymeleaf template hello.html, access variables using th: to access;
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title></Head><Body><H1>Hello,</H1><H1Th:text= "${name}"/></Body></HTML>
⑤, start the project, and visit Http://localhost:8080/getThymeleaf as follows:
The directory structure is as follows:
Complete Example: https://gitee.com/lfalex/spring-boot-example
Copyright NOTICE: This article for Bo Master original article, reproduced please indicate the source, thank you!
(ii) Springboot Foundation-access to static resources and use of thymeleaf template engines