[聚合文章] springboot使用thymeleaf为模板

JavaScript 1900-01-01 9 阅读

jsp已经明显跟不上时代发展了,freemarker用的够够的?换thymeleaf试试吧。springboot官方推荐的是freemarker和thymeleaf,而thymeleaf相对于freemarker更让人感觉强大的,是他可以动态替换标签内静态内容,这样前端可以安心写页面,后台可以安心撸接口,只需要把变量替换一下即可,这种理念,不知道是VUE抄袭了thymeleaf还是thymeleaf抄袭了VUE,不过无所谓了 ,对于我们广大码奴来说,实用就好

壹、pom引入

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

贰、application.properties添加thymeleaf配置

spring.thymeleaf.cache=falsespring.thymeleaf.check-template=truespring.thymeleaf.check-template-location=truespring.thymeleaf.content-type=text/htmlspring.thymeleaf.enabled=truespring.thymeleaf.encoding=utf-8spring.thymeleaf.mode=HTML5spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.html

叁、编写html

<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"                xmlns:th="http://www.thymeleaf.org"><head>    <title>demo</title></head><body><p>这是第一段</p><p th:text="${textValue}">这是第二段</p></body></html>

肆、测试类

package com.mos.easyboot.admin.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("demo")public class DemoController {    @RequestMapping("index")    public String index(Model model){        String textValue = "上士闻道,仅能行之;中士闻道,若存若亡;下士闻道,大笑之。" +                "不笑不足以为道。" +                "故建言有之:明道若昧;进道若退;夷道若颣(lei);上德若谷,大白若辱,广德若不足,建德若偷,质真若渝;大方无隅;大器免成;大音希声;大象无形。" +                "道隐无名。" +                "夫唯道,善始且善成。";        model.addAttribute("textValue",textValue);        return "demo/demo";    }
                

注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。