栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot2

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot2

return “index1”;

}

@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;

}

@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=

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/434658.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号