thymeleaf快速入门

开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了。其中Thymeleaf是SpringBoot官方所推荐使用的,下面来谈谈Thymeleaf一些常用的语法规则。

添加Thymeleaf依赖

要想使用Thhymeleaf,首先要在pom.xml文件中单独添加Thymeleaf依赖。

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

Spring Boot默认的模板页面路径在src/main/resources/templates,默认的页面文件后缀是.html

数据显示

在MVC的开发过程中,我们经常需要通过Controller将数据传递到页面中,让页面进行动态展示。

显示普通文本

创建一个Controller对象,在其中进行参数的传递

```java @Controller public class ThymeleafController {

@RequestMapping(value = "show", method = RequestMethod.GET)
public String show(Model model){
    model.addAttribute("uid","123456789");
    model.addAttribute("name","Jerry");
    return "show";
}

} ```

在SpringBoot默认的页面路径下创建show.html文件,内容如下

java <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>SpringBoot模版渲染</title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> </head> <body> <p th:text="'用户ID:' + ${uid}"/> <p th:text="'用户名称:' + ${name}"/> </body> </html>

p标签中有th:text属性,它表示显示一个普通的文本信息。

显示带有样式的普通文本

如果我们想要传递到的页面的信息,它本身是带有CSS样式的,这个时候如何在页面中将携带的样式信息也显示出来?此时我们的控制器方法这样写。

java @RequestMapping(value = "showStyle", method = RequestMethod.GET) public String showStyle(Model model){ model.addAttribute("uid","123456789"); model.addAttribute("name","<span style='color:red'>Jerry</span>"); return "show_style"; }

此时页面中需要借助th:utext属性进行显示

```bash


访问controller方法 访问静态页面 ``` #### 逻辑处理 所有的页面模版都存在各种基础逻辑处理,例如:判断、循环处理操作。在 Thymeleaf 之中对于逻辑可以使用如下的一些运算符来完成,例如:and、or、关系比较( >、=、<=、==、!=、lt()、le(<=)、ge(>=)、eq(=)、ne(!=) )。 通过控制器传递一些属性内容到页面之中: ```xml 未成年人! 欢迎小六子来访问! ``` ```xml 成年人 ``` ```jsx

uid为101的员工来了

uid为102的员工来了

没有匹配成功的数据!

``` #### 集合处理 在 thymeleaf 之中提供有相应的集合的处理方法,例如:在使用 List 集合的时候可以考虑采用 get()方法获取指定索引的数据,那么在使用 Set 集合的时候会考虑使用 contains()来判断某个数据是否存在,使用 Map 集合的时候也希望可以使用 containsKey()判断某个 key 是否存在,以及使用get()根据 key 获取对应的 value。 - 控制器方法向页面传递一些数据,以供操作 ```java @RequestMapping(value = "/user/set", method = RequestMethod.GET) public String set(Model model) { Set allNames = new HashSet () ; List allIds = new ArrayList () ; for (int x = 0 ; x < 5 ; x ++) { allNames.add("boot-" + x) ; allIds.add(x) ; } model.addAttribute("names", allNames) ; model.addAttribute("ids", allIds) ; model.addAttribute("mydate", new java.util.Date()) ; return "user_set" ; } ``` - 页面数据处理 ```java


allMembers = new HashMap (); for (int x = 0; x < 10; x++) { User vo = new User(); vo.setUid(101L + x); vo.setName("赵四 - " + x); vo.setAge(9); vo.setSalary(99999.99); vo.setBirthday(new Date()); allMembers.put("mldn-" + x, vo); } model.addAttribute("allUsers", allMembers); return "user_map"; } @RequestMapping(value = "/user/list", method = RequestMethod.GET) public String list(Model model) { List allMembers = new ArrayList (); for (int x = 0; x < 10; x++) { User vo = new User(); vo.setUid(101L + x); vo.setName("赵四 - " + x); vo.setAge(9); vo.setSalary(99999.99); vo.setBirthday(new Date()); allMembers.add(vo) ; } model.addAttribute("allUsers", allMembers); return "user_list"; } } ``` - list类型数据遍历 ```java
No.UID姓名年龄偶数奇数
``` - map类型数据遍历 ```java
No.KEYUID姓名年龄偶数奇数
``` #### 页面引入 我们常常需要在一个页面当中引入另一个页面,例如,公用的导航栏以及页脚页面。thymeleaf中提供了两种方式进行页面引入。 - th:replace:将当前标签中的内容替换为模板中的内容 - th:include:加载模板内容 1. 新建需要被引入的页面文件,路径为"src/main/view/templates/commons/footer.html" ```java

吃葡萄不吐葡萄皮

``` `th:fragment`属性用来定义一个模板,声明该属性的div为模板片段,常与`th:include`,`th:replace`一起使用 2. 可以看到页面当中还存在一个变量projectName,这个变量的值可以在引入页面中通过`th:with="projectName=百度"`传过来。 ```dart

今天是:

```

  • 一些环境相关对象

| 对象 | 作用 | | ----------------- | --------------------------------------------- | | #ctx | 获取Thymeleaf自己的Context对象 | | #requset | 如果是web程序,可以获取HttpServletRequest对象 | | #response | 如果是web程序,可以获取HttpServletReponse对象 | | #session | 如果是web程序,可以获取HttpSession对象 | | #servletContext | 如果是web程序,可以获取HttpServletContext对象 |

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值