分享知识-快乐自己:springboot之thymeleaf (1):简单的thymeleaf例子
2018-09-10 21:20
696 查看
之前搞springboot时,发现spring很推荐thymeleaf,所以看了看学了学,感觉不错,做个笔记先。
做个简单和例子,项目是springboot,所以引入themeleaf相关包
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
接着写个controller
@RequestMapping("/") public String hello(Model model, HttpServletRequest request) { // 文本表达式 model.addAttribute("name", "ywj"); // 获取对象属性 model.addAttribute("user", new User("ywj3")); // 获取session文本 request.getSession().setAttribute("sname", "name from session"); // 获取session对象属性 request.getSession().setAttribute("suser", new User("name from session")); // 显示html效果 model.addAttribute("html", "<b>BBB</b>"); // 数组 String[] arr = {"a", "b"}; model.addAttribute("arr", arr); // url model.addAttribute("url", "http://www.baidu.com"); model.addAttribute("today", new Date()); return "index"; }
接着在src/main/resources/templates中创建一个index.html,是html,不是jsp 然后看看thymeleaf语法:
/// <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Hello Thymeleaf!</title> </head> <body> 文本表达式: <span th:text="${name}" /><hr/> 文本表达式,获取对象属性: <span th:text="${user.name}" /><hr/> session中文本: <span th:text="${session.sname}" /><hr/> session中对象属性: <span th:text="${session.suser.name}" /><hr/> html效果: <span th:utext="${html}" /><hr/> url: <a th:href="@{${url}}">baidu</a><hr/> url跳转 <a th:href="@{/b}">效果是:127.0.0.1/项目名/b</a><hr/> url还参数,效果:/c?a=aaa&b=bbb.还有一种@{/c/{a}(a='aaa'))} = /c/aaa <a th:href="@{/c(a='aaa',b='bbb')}">url带参数</a><hr/> url同一服务器下请求别的项目,比如项目A和项目thymeleaf1是同一个服务器里页的,项目A请求项目thymeleaf1里的方法,就用~/thymeleaf1/方法 <a th:href="@{~/thymeleaf1/c(a='aaa',b='bbb')}">url带参数</a><hr/> 数字可加减乘除余 <span th:text="1" />加:1+2=<span th:text="${1+2}" />减:2-1=<span th:text="${2-1}" /> 乘:2*3=<span th:text="${2*3}" />除:6/2=<span th:text="${6/2}" />余:3%2=<span th:text="${3%2}" /><hr/> 数字比较:<!-- gt(>),lt(<),ge(>=),le(<=),not(!) eq(==),neq/ ne(!=) --> <span th:if="${2 > 1}">2大于1</span> <span th:if="2 < 1">2大于1</span> 时间格式: <span th:text="${#dates.format(today, 'yyyy-MM-dd HH:mm:ss')}">13 May 2011</span> </body> </html>///
(注:更多详解请跳转:https://blog.csdn.net/u013845177/article/category/7226505)
相关文章推荐
- 分享知识-快乐自己:SpringBoot集成热部署配置(一)
- 分享知识-快乐自己:SpringBoot结合使用拦截器(判断是否用户是否已登陆)
- 分享知识-快乐自己:spring_Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson
- [置顶] 自己写了好久的SSM个人博客。。。记录下,并分享 源码----(SpringBoot+Thymeleaf+layui+mybatis)
- 分享知识-快乐自己:Spring切入点的表达式和通知类型
- java鬼混笔记:springboot之thymeleaf 1:简单的thymeleaf例子
- 分享知识-快乐自己:Spring中的(三种)异常处理机制
- SpringBoot + thymeleaf 实现简单的登陆验证
- springboot整合mybatis最简单的例子
- Spring Boot—— Thymeleaf (gradle) 的简单使用
- 分享知识-快乐自己:Hibernate中的 quert.list() 与 quert.iterate() 方法区别
- Swagger(一) SpringBoot整合Swagger2简单的例子
- 分享知识-快乐自己:IDEA下maven编译打包Java项目成jar包但是resource下配置文件无法编译
- 分享知识-快乐自己:微服务的注册与发现(基于Eureka)
- 分享知识-快乐自己:SpringMvc中的单多文件上传及文件下载
- Spring boot WebSocket stomp简单例子
- 分享知识-快乐自己:论 Mybatis中的关联关系(一对多,多对一,多对多)
- springboot+mybatis的简单的入门例子
- 笔记:Spring boot+Thymeleaf+spring-data-jpa简单项目搭建
- Spring Boot—— Thymeleaf (gradle) 的简单使用