美文网首页
Thymeleaf-遍历

Thymeleaf-遍历

作者: 通灵路耳 | 来源:发表于2020-03-07 17:03 被阅读0次
图片.png
1、基于“置顶-SpringBoot-最终整合”进行
2、控制器

package com.llhc.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.llhc.model.Product;
@Controller
@RequestMapping("system")
public class TestThymeleafController {
    /**
     * 
     * Thymeleaf+表达式
     * @author 开发者
     * 
     */
    @RequestMapping("thy1")
    public String thy1(Model model){
        String s = "<p style='color:red'>红色文字</p>";
        Product p = new Product("实体");
        boolean b = true;
        List<Product> l = new ArrayList<>();
        l.add(new Product("张三"));
        l.add(new Product("离散"));
        
        model.addAttribute("host1",s);
        model.addAttribute("host2",p);
        model.addAttribute("host3",b);
        model.addAttribute("host4",l);
        return "hello";
    }
}


3、在resources下的templates文件夹,创建hello.html

<!DOCTYPE HTML>
<!-- 声明当前文件是Thymeleaf,可以用th开头 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Thymeleaf</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all" href="../../webapp/static/css/style.css" th:href="@{/static/css/style.css}"/>
    <script type="text/javascript" src="../../webapp/static/js/thymeleaf.js" th:src="@{/static/js/thymeleaf.js}"></script>
    <script>
        testFunction();
    </script>
    <style>
        h2{
            text-decoration: underline;
            font-size:0.9em;
            color:gray;
        }
    </style>    
</head>
<body>
    <!-- 普通遍历 -->
    <div class="showing">
            <h2>普通遍历</h2>
            <table>
                <thead>
                    <tr>
                        <th>产品名称</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="p: ${host4}">
                        <td th:text="${p.name}"></td>
                    </tr>
                </tbody>
            </table>
    </div>
    <!-- 带状态遍历  -->
    <div class="showing">
            <h2>带状态遍历</h2>
            <table>
                <thead>
                    <tr>
                        <th>产品名称</th>
                    </tr>
                </thead>
                <tbody>
                <!-- 先奇数 后偶数 -->
                    <tr th:class="${status.even}?'even':'odd'" th:each="p,status: ${host4}">
                    <!--  -->
                        <td  th:text="${status.index}"></td>
                        <td th:text="${p.name}"></td>
                    </tr>
                </tbody>
            </table>
    </div>
</body>
</html>

相关文章

网友评论

      本文标题:Thymeleaf-遍历

      本文链接:https://www.haomeiwen.com/subject/tmocdhtx.html