【问题标题】:How to use a Java for loop in an html page如何在 html 页面中使用 Java for 循环
【发布时间】:2020-01-29 10:17:56
【问题描述】:

在我的系统中,我需要在 html 表中使用 java for 循环来打印从 8.00 到 17.00 的时间。有人能解释一下怎么做吗。谢谢。

HTML 文件

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
    <link rel="stylesheet" type="text/css" href="static/css/timeTable.css" th:href="@{/css/timeTable.css}">
    <meta charset="UTF-8">
    <title>Time Table</title>
</head>
<body>

<div class="container" style="margin-top:30px">
    <br><br>
    <table border="1" class="table2">
        <thead>
        <tr>
        </tr>
        <br>
        <tr>
            <td></td>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thursday</th>
            <th>Friday</th>
        </tr>
        </thead>
        <tbody>

        <tr>
            <% for(int i=0;i<17;i++){
            <td> i.":00 - ".($i+1).":00" </td>
        }
        </tr>

        </tbody>
    </table>
</div>

</body>
</html>

【问题讨论】:

  • 那必须是一个 JSP 文件(尽管 javascript 也可以)。你也错过了结束%&gt;
  • 如何添加jsp文件?谢谢
  • 看起来您正在使用百里香叶。因此,使用 thymeleaf,您可以使用 th:each 进行迭代,而无需 jsp 文件。
  • 是的,我正在使用百里香叶。我如何使用 th:each 进行类似的迭代

标签: java html spring-boot thymeleaf


【解决方案1】:

您应该按如下方式安装 JSTL:

%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 然后遍历列表如下:

<c:forEach var = "i" begin = "8.00" end = "17.00">
         Item <c:out value = "${i}"/><p>
      </c:forEach>

【讨论】:

  • .jsp 文件而已。
  • 或者你可以使用javascript在html页面上渲染。
【解决方案2】:

我可以通过以下方式做到这一点。感谢所有试图提供帮助的人

<option th:each="i : ${#numbers.sequence( 8, 17)}">
     <tr>
         <th th:text="${ i }+':00 - ' + ${ i+1 }+':00'"></th>
     </tr>
 </option>

【讨论】:

    【解决方案3】:

    您为什么不使用 Javascript 而不是发疯地发明类似的东西? Javascript 是一种强大的语言,也有一个 for 循环。 让到属于前面的前面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多