2、输出 html
3、输出到 a 标签属性中userid 是后台设置的值4、遍历 list
当前位置:   article > 正文

thymeleaf 用法总结_thymeleaf控制台直接输出

thymeleaf控制台直接输出

1、输出 text

<div th:text="${user.age}"></div>

2、输出 html

<div th:utext="${user.name}"></div>

3、输出到 a 标签属性中

userid 是后台设置的值

<a  th:href="@{/user/detail/{id}(id=${userid})}"></a>

4、遍历 list

  1. <a th:each="u : ${userList}">
  2. <p>
  3. <span th:text="${u.name}"></span>
  4. <span th:text="${u.age}"></span>
  5. </p>
  6. </a>

代码测试

这里使用 springboot项目

定义 User实体类
 

  1. package com.learn.bean;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. /**
  6. * User
  7. *
  8. * @author wsjz
  9. * @date 2021/07/15
  10. */
  11. @NoArgsConstructor
  12. @AllArgsConstructor
  13. @Data
  14. public class User {
  15. private String id;
  16. private String name;
  17. private Integer age;
  18. }

定义controller

  1. package com.learn.controller;
  2. import com.learn.bean.User;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.servlet.ModelAndView;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. /**
  9. * DemoController
  10. *
  11. * @author wsjz
  12. * @date 2021/07/15
  13. */
  14. @Controller
  15. public class IndexController {
  16. @RequestMapping("/")
  17. public ModelAndView index() {
  18. ModelAndView mav = new ModelAndView();
  19. mav.addObject("userList", userList());
  20. mav.addObject("user", user());
  21. mav.setViewName("index");
  22. return mav;
  23. }
  24. private List<User> userList() {
  25. List<User> userList = new ArrayList<>();
  26. User user1 = new User("1", "贾元春", 18);
  27. User user2 = new User("2", "贾迎春", 17);
  28. User user3 = new User("3", "贾探春", 16);
  29. User user4 = new User("4", "贾惜春", 15);
  30. userList.add(user1);
  31. userList.add(user2);
  32. userList.add(user3);
  33. userList.add(user4);
  34. return userList;
  35. }
  36. private User user() {
  37. User user = new User("5", "<p style='color:red'>贾宝玉<p>", 16);
  38. return user;
  39. }
  40. }
thymeleaf 页面
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <a th:href="@{/user/detail/{id}(id=${u.id})}" th:each="u : ${userList}">
  9. <p>
  10. <span th:text="${u.name}"></span>
  11. <span th:text="${u.age}"></span>
  12. </p>
  13. </a>
  14. <hr>
  15. <div th:utext="${user.name}"></div>
  16. <div th:text="${user.age}"></div>
  17. </body>
  18. </html>

运行效果

至此完 

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

闽ICP备14008679号