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

  • 相关阅读:
    Line of Sight 计算几何基础
    Hash算法详解
    高效mysql的习惯(程序员版本)
    thymeleaf初步使用
    @Transactional注解事务不起作用
    泛型的理解
    Git&GitHun 命令合集
    springboot引入thymeleaf
    springboot静态资源映射
    springboot的配置文件
  • 原文地址:https://www.cnblogs.com/chenweichu/p/9292021.html
Copyright © 2011-2022 走看看