zoukankan      html  css  js  c++  java
  • springboot整合thymeleaf模板引擎

    1、架包依赖引入:pom.xml

    <dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.22</version>
    </dependency>

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

    2、配置文件:application.properties

    #thymelea模板配置
    spring.thymeleaf.prefix=classpath:/static/page/
    spring.thymeleaf.suffix=.html
    spring.thymeleaf.check-template=false
    spring.thymeleaf.mode=LEGACYHTML5
    spring.thymeleaf.cache=false

    3、controller

    @RestController
    @RequestMapping("test")
    public class TestController {
    
        @RequestMapping("index")
        public ModelAndView index(ModelMap model) {
            ModelAndView mv = new ModelAndView();
            model.addAttribute("message" ,"Hello World");
            mv.setViewName("index"); //页面路径:/static/page/index.html
            return mv;
        }
    
    
        @RequestMapping("demo")
        public String demo() {
            return "hahahaha";
        }
    
    }

    访问路径:

    localhost:8080/test/index  返回:页面

    localhost:8080/test/demo  返回:hahahaha

    如果:/test/demo能返回,/test/index没返回,这样就要看thymeleaf是否已经被引入。

    注意:

    1、默认情况下,thymeleaf对html的检查过于严格,导致springboot启动失败。

    解决方法:通常会设置spring.thymeleaf.mode=LEGACYHTML5,然后再配合nekohtml(pmo.xml导入相关架构依赖)就能实现。

  • 相关阅读:
    POLARDB与其他关系型数据库对比
    美团关于分布式ID实践方案
    CRM、DMP、CDP概念解析
    TIDB简介
    美团关于分布式ID实践方案细节
    美团在TIDB方面的实践
    设计模式之桥梁模式
    【转】Hibernate和IBatis对比
    Eclipse插件安装
    【转】揭开正则表达式的神秘面纱
  • 原文地址:https://www.cnblogs.com/chenweichu/p/9292021.html
Copyright © 2011-2022 走看看