The first step: Pom.xml Join the dependency
<!--HTML templates--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
If an error occurs, the following:
Exception encountered during context initialization-cancelling refresh attempt: Org.springframework.beans.factory.BeanCreationException:
Error Creating Beanwith Name' Viewresolver ' definedInchClass path Resource [org/springframework/boot/autoconfigure/web/webmvcautoconfiguration$ Webmvcautoconfigurationadapter.Class]: Initializationof Bean failed; NestedException is org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating beanwith Name' org.springframework.boot.autoconfigure.thymeleaf.thymeleafautoconfiguration$thymeleaf3configuration$ Thymeleaf3viewresolverconfiguration ': Unsatisfied dependency expressed through constructor parameter1; NestedException is org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating beanwith Name' Org.springframework.boot.autoconfigure.thymeleaf.thymeleafautoconfiguration$thymeleafdefaultconfiguration ': Unsatisfied dependency expressed through constructor parameter0; NestedException is org.springframework.beans.factory.BeanCreationException:Error creating beanwith Name ' defaulttemplateresolver ' defined in class path resource [org/springframework/boot/autoconfigure/thymeleaf/ Thymeleafautoconfiguration$thymeleaf3configuration$defaulttemplateresolverconfiguration. class]: Bean instantiation via factory method failed; nested Span class= "Hljs-keyword" >exception is org.springframework.beans.BeanInstantiationException:Failed to instantiate [Org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver]: Factory Span class= "Hljs-keyword" >method ' Defaulttemplateresolver ' threw exception; nested exception is java.lang.NoSuchMethodError: Org.thymeleaf.templateresolver.TemplateResolver.checkNotInitialized () V
We should modify the properties configuration
<properties> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> < Thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version></properties>
Step Two: Modify the Yml file
Spring: thymeleaf: mode:html5 encoding:utf-8 content-type:text/html cache:false
Step Three: Modify the Controller
@GetMapping ("/hello") String Test (HttpServletRequest request) { // logical processing Request.setattribute ("name", "Java"); return "/hello"; }
< Span class= "Hljs-keyword" >< Span class= "hljs-string" >
Fourth Step: Modify HTML on resource/templates/hello.html
<!DOCTYPE HTML><HTMLxmlns:th= "http://www.thymeleaf.org"><Head> <title>Hello world!</title></Head><Body><spanTh:text= "' Welcome to our application, ' + ${name} + '! '"></span></Body></HTML>
Fifth step: Browser input to see the effect
Spring Boot Thymeleaf Configuration