Spring Boot Thymeleaf Configuration

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.