Search code examples
javahtmlspring-bootthymeleaf

How to use a Java for loop in an html page


In my system i need to use a java for loop inside an html table to print time from 8.00-17.00. Can someone please explain me how to do it.Thank you.

Html file

<!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>

Solution

  • I was able to do it by using the following way. Thx everyone who tried to help

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