当前位置:   article > 正文

SpringBoot学习(二)视图技术Thymeleaf_spring boot 和 thymeleaf实现图片轮播

spring boot 和 thymeleaf实现图片轮播

Thymeleaf是一个现代的服务器端 Java 模板引擎,适用于 Web 和独立环境。

官方网址

Thymeleafhttps://www.thymeleaf.org/pom.xml文件Thymeleaf依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>

在模板中加入解析

  1. <!DOCTYPE html>
  2. <html lang="zh_cn" xmlns:th="http://www.thymeleaf.org/"
  3. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

后面这个是要用到spring security的相关数据时要用到的命名空间,暂且不提。 

application.properties文件中关于thymeleaf的配置项

  1. spring.thymeleaf.model=HTML5
  2. spring.thymeleaf.encoding=UTF-8
  3. spring.thymeleaf.content-type=text/html
  4. spring.thymeleaf.cache=false

常用th标签

  1. <p th:text="${name}"></p>
  2. 显示文本

th:object

  1. <p th:object="${object}"></p>
  2. 显示对象

th:action

  1. <form th:action="@{/person/}+${person.id}" method="post"></form>
  2. 用于指定表单提交地址

th:value

th:field

URL写法

th:href="@{http://www.baidu.com}"

th:href="@{/}"

th:href="@{css/bootstrap.min.css}"

条件求值

th:if th:unless

th:switch

th:case

运算符

+ - * /

<p th:text="1+2"></p>

遍历

th:each

  1. <div th:each="item:${items}">
  2. <span th:text="${item.name}"></span>
  3. </div>

处理公共代码

th:fragment th:include th:replace

新建一个简单的model类 User

  1. public class User {
  2. private long id;
  3. private String name;
  4. private int age;
  5. public long getId() {
  6. return id;
  7. }
  8. public void setId(long id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. }

创建控制器UserController

  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.servlet.ModelAndView;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. @Controller
  7. public class UserController {
  8. @GetMapping("/user")
  9. public ModelAndView hello(){
  10. List<User> list = new ArrayList<User>();
  11. User user = new User();
  12. user.setName("张三");
  13. user.setAge(17);
  14. list.add(user);
  15. User lisi = new User();
  16. lisi.setName("李四");
  17. lisi.setAge(19);
  18. list.add(lisi);
  19. User wangwu = new User();
  20. wangwu.setName("王五");
  21. wangwu.setAge(19);
  22. list.add(wangwu);
  23. ModelAndView model = new ModelAndView("user");
  24. model.addObject("users",list);
  25. return model;
  26. }
  27. }

这里的model的名字就是html文件的名字,一开始没搞明白。

创建view 就是html user.html

  1. <!DOCTYPE html>
  2. <html lang="zh_cn" xmlns:th="http://www.thymeleaf.ort">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>this is user test page</title>
  6. </head>
  7. <body>
  8. <table border="1">
  9. <tr>
  10. <th>姓名</th>
  11. <th>年龄</th>
  12. </tr>
  13. <tr th:each="user : ${users}">
  14. <td th:text="${user.name}"></td>
  15. <td th:text="${user.age}"></td>
  16. </tr>
  17. </table>
  18. </body>
  19. </html>

运行显示。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/204923
推荐阅读
相关标签
  

闽ICP备14008679号