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

猜你喜欢

转载自www.cnblogs.com/chenweichu/p/9292021.html