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导入相关架构依赖)就能实现。

  • 相关阅读:
    解决vue空格换行报错问题
    基本的项目开发流程(前后端开发)
    whl包构建
    Python虚拟环境创建
    页面适配 JS
    SpringBoot整合Ehcache3
    SpringBoot文件分片上传
    SpringBoot访问jar包静态文件
    SpringBoot整合Minio文件存储
    SpringBoot多环境配置文件打包
  • 原文地址:https://www.cnblogs.com/chenweichu/p/9292021.html
Copyright © 2011-2022 走看看