SpringBoot2

return “index1”;

}

/**

  • 方案二

  • @return

*/

@GetMapping(“index2”)

public Object index2() {

ModelAndView modelAndView = new ModelAndView("/index2");

modelAndView.addObject(“id”, “2”);

modelAndView.addObject(“title”, “SpringBoot Thymeleaf2”);

modelAndView.addObject(“author”, “Java程序鱼”);

return modelAndView;

}

/**

  • 方案三

  • @param modelMap

  • @return

*/

@GetMapping(“index3”)

public String index3(ModelMap modelMap) {

modelMap.addAttribute(“id”, “3”);

modelMap.addAttribute(“title”, “SpringBoot Thymeleaf3”);

modelMap.addAttribute(“author”, “Java程序鱼”);

return “index3”;

}

}

六、编写html

=======================================================================

在 src/main/resources/templates 下创建index1.html、index2.html、index3.html三个文件,代码都一样。

index1 id title author

在这里插入图片描述

七、语法

===================================================================

th:each


Thymeleaf 中使用 th:each 来进行 for 循环遍历

@Controller

public class BlogController {

@GetMapping(“each”)

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

public String each(Model model) {

ArrayList list = new ArrayList<>();

list.add(new Blog(1, “each1”, “java程序鱼1”));

list.add(new Blog(2, “each2”, “java程序鱼2”));

list.add(new Blog(3, “each3”, “java程序鱼3”));

model.addAttribute(“list”, list);

return “each”;

}

}

【each.html】

ID 标题 作者

在这里插入图片描述

th:if


Thymeleaf 中使用 th:if 和 th:unless 属性进行条件判断,th:if 是表达式中的条件成立显示内容,th:unless 是表达式中的条件不成立才显示内容。

@Controller

public class BlogController {

@GetMapping(“if”)

public String ifHtml(Model model) {

model.addAttribute(“id”, 1);

return “if”;

}

}

【if.html】

th:if id==1
th:if id!=1
th:unless id==1
th:unless id!=1

在这里插入图片描述

th:replace&th:include


th:include,布局标签,替换内容到引入的文件

th:replace,布局标签,替换整个标签到引入的文件

我们在开发中需要把通用的部分都提取出来,例如文章的头部和底部,把文章的头部和底部弄成单独的页面,然后让其他页面包含头部和底部,这就是代码复用思维。

【include.html】

Thymeleaf include

博客正文

【commonFooter.html】

通用代码-底部

博客的底部

【commonHead.html】

通用代码-头部

博客的头部

在这里插入图片描述

八、Thymeleaf的默认参数配置

=================================================================================

在application.properties中可以配置thymeleaf模板解析器属性

THYMELEAF (ThymeleafAutoConfiguration)

#开启模板缓存(默认值:true)

spring.thymeleaf.cache=true

#Check that the template exists before rendering it.

spring.thymeleaf.check-template=true

#检查模板位置是否正确(默认值:true)

spring.thymeleaf.check-template-location=true

#Content-Type的值(默认值:text/html)

spring.thymeleaf.content-type=text/html

#开启MVC Thymeleaf视图解析(默认值:true)

spring.thymeleaf.enabled=true

#模板编码

spring.thymeleaf.encoding=UTF-8

#要被排除在解析之外的视图名称列表,用逗号分隔

spring.thymeleaf.excluded-view-names=

#要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5)

spring.thymeleaf.mode=HTML5

#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)

spring.thymeleaf.prefix=classpath:/templates/

#在构建URL时添加到视图名称后的后缀(默认值:.html)

spring.thymeleaf.suffix=.html

#Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。

spring.thymeleaf.template-resolver-order=

#可解析的视图名称列表,用逗号分隔

spring.thymeleaf.view-names=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值