2、输出 html
3、输出到 a 标签属性中userid 是后台设置的值4、遍历 list赞
踩
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
- <a th:each="u : ${userList}">
- <p>
- <span th:text="${u.name}"></span>
- <span th:text="${u.age}"></span>
- </p>
- </a>
代码测试
这里使用 springboot项目
定义 User实体类
- package com.learn.bean;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- /**
- * User
- *
- * @author wsjz
- * @date 2021/07/15
- */
- @NoArgsConstructor
- @AllArgsConstructor
- @Data
- public class User {
-
- private String id;
-
- private String name;
-
- private Integer age;
-
- }
定义controller
- package com.learn.controller;
-
- import com.learn.bean.User;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * DemoController
- *
- * @author wsjz
- * @date 2021/07/15
- */
- @Controller
- public class IndexController {
-
- @RequestMapping("/")
- public ModelAndView index() {
- ModelAndView mav = new ModelAndView();
-
- mav.addObject("userList", userList());
- mav.addObject("user", user());
- mav.setViewName("index");
- return mav;
- }
-
- private List<User> userList() {
- List<User> userList = new ArrayList<>();
-
- User user1 = new User("1", "贾元春", 18);
- User user2 = new User("2", "贾迎春", 17);
- User user3 = new User("3", "贾探春", 16);
- User user4 = new User("4", "贾惜春", 15);
-
- userList.add(user1);
- userList.add(user2);
- userList.add(user3);
- userList.add(user4);
-
- return userList;
- }
-
- private User user() {
- User user = new User("5", "<p style='color:red'>贾宝玉<p>", 16);
- return user;
- }
- }
thymeleaf 页面
- <!DOCTYPE html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
-
- <a th:href="@{/user/detail/{id}(id=${u.id})}" th:each="u : ${userList}">
- <p>
- <span th:text="${u.name}"></span>
- <span th:text="${u.age}"></span>
- </p>
- </a>
-
- <hr>
- <div th:utext="${user.name}"></div>
- <div th:text="${user.age}"></div>
- </body>
- </html>
运行效果
至此完
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。