springboot 分析源码欢迎页和图标-> thymeleaf模板引擎常用语法->扩展-编程思维

欢迎页:

 

icon:

 注意点:

 thymeleaf模板引擎

1.使用thymeleaf模板引擎前要导入对应依赖包

2.阅读源码:

根据源码说明我们可以将html文件放置在templates目录下,然后通过controller进行跳转即可

 controller类:

//在templates下的东西需要通过controller类来跳转,
// 需要导入thymeleaf对应的依赖包
@Controller
public class IndexController {
    //跳转到templates目录下的测试页面
    @RequestMapping("/t")
    public String test1(){
        return "test";
    }
}
View Code

 

结果:

 thymeleaf语法介绍

这里测试一下在controller类设置一个属性给前端接收并展示,

首先要搞清楚在thymeleaf中是如何在前端页面展示接收到的数据的,查看源码发现标签th:text和th:each

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>test thymeleaf!!</h1>
<div th:text="${msg}"></div>
<div th:each="user:${users}" th:text="${user}"></div>
</body>
</html>
View Code

controller测试代码:

//在templates下的东西需要通过controller类来跳转,
// 需要导入thymeleaf对应的依赖包
@Controller
public class IndexController {
    //跳转到templates目录下的测试页面
    @RequestMapping("/t1")
    public String test1(){
        return "test";
    }
    @RequestMapping("/t2")
    public String test2(Model model){
        //在这里设置一个属性给前端接收并展示
        model.addAttribute("msg","test2---hello,thymeleaf!");
        return "test";

    }
    @RequestMapping("/t3")
    public String test3(Model model){
        //在这里设置一个属性给前端接收并展示
        model.addAttribute("msg","<h1>test3---hello,thymeleaf!</h1>");
        model.addAttribute("users", Arrays.asList("lian","xiaoming","xiaohong"));
        return "test";
    }
}
View Code

 

 运行结果(部分):

总结一些常用的thymeleaf标签:

th:text="${emp.getId()}" 将数据以文本形式展示在前端

th:each="emp:${emps}"遍历列表emps中的每一项

th:checked="${emp.getGender()==0}"判断,暂时不清楚

th:value="${#dates.format(emp.getBirth(),'yyyy-MM-dd')}" 暂时不太清楚,暂且理解的是取出数据传递给后端

 

版权声明:本文版权归作者所有,遵循 CC 4.0 BY-SA 许可协议, 转载请注明原文链接
https://www.cnblogs.com/MyBlogs-joyiyii/p/17367618.html

springboot 项目国际化+登录拦截器-编程思维

项目页面国际化 1.语言配置文件 需要下载插件Resource Bundle Editor  新建国际目录i18n   在properties配置文件中自定义    2.前端index页面要设置语言参数传递给后端,切换中英文  3.自定义地区解析器MyLocaleResolver后端接收并处理  4.自定义了

springboot 整合jdbc-编程思维

在springboot底层无论关系型还是非关系型数据库都是用spring-data进行交互   新建: 通过spring initialer勾选重要依赖jdbc api和mysql driver: 源码分析-设置数据源配置: 通过源码可以看到我们一个通过spring.datasource.xxx来设置我们的yaml配

springboot 自动装配的原理-编程思维

 自动装配原理 问题就是为什么我们直接导入依赖就可以使用了,甚至不用配置web.xml,tomcat等,springboot内部是如何实现的? 主启动类上的注解@SpringBootApplication里有三个重要注解: @SpringBootConfiguration@EnableAutoConfiguratio

springboot 跳转到网页上的两种实现方式(转发与重定向详细对比)-编程思维

1.情景展示 虽然现在流行的是前后端分离,后端开发与前端往往只进行数据交互,不需要参与对网页跳转的控制及网页内容的开发。 但是,由服务器(后端)跳转到客户端(浏览器)或者从A服务器跳到B服务器是一项基本的能力。 在项目开发中,真正遇到的时候,该如何实现? 哪种实现方式更好? 2.具体分析 无论是springboot,S