thymeleaf index.html

This commit is contained in:
Patrick Haßel 2024-10-16 14:36:19 +02:00
parent 45dfaf5f5c
commit 297e4b4029
3 changed files with 82 additions and 6 deletions

View File

@ -34,10 +34,16 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>

View File

@ -1,5 +1,6 @@
package de.ph87.data.series;
import jakarta.annotation.Nullable;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -8,16 +9,40 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.stream.Stream;
@Slf4j
@Controller
@Transactional
@RequiredArgsConstructor
public class SeriesController {
private final SeriesRepository seriesRepository;
@GetMapping("/")
public String index(@NonNull final Model model) {
// model.addAttribute("series", getAll());
final Stream<Series> series = seriesRepository.findAll().stream().sorted(Comparator.comparing(Series::getName));
model.addAttribute("series", series);
return "index";
}
@Nullable
public static String numberFormat(@Nullable final Double value, final int decimals) {
if (value == null) {
return null;
}
return "%%.%df".formatted(decimals).formatted(value);
}
@Nullable
public static String dateTimeFormat(@Nullable final ZonedDateTime date) {
if (date == null) {
return null;
}
return date.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}
}

View File

@ -1,5 +1,50 @@
<table>
<!-- <tr th:each="serie: ${series}">-->
<!-- <td th:text="${serie.name}"></td>-->
<!-- </tr>-->
</table>
<!DOCTYPE html>
<html lang="de">
<head>
<meta http-equiv="refresh" content="5">
<title>Data</title>
<style>
.section {
margin: 10px;
}
table {
font-family: monospace;
border-collapse: collapse;
}
td, th {
border: 2px solid white;
padding: 10px;
background-color: lightgray;
}
.value {
text-align: right;
border-right: none;
padding-right: 0;
}
.unit {
border-left: none;
}
</style>
</head>
<body>
<div class="section">
<table>
<tr th:each="serie: ${series}">
<td class="name" th:text="${serie.name}"></td>
<td class="mode" th:text="${serie.mode}"></td>
<td class="value" th:text="${@seriesController.numberFormat(serie.lastValue, 1)}"></td>
<td class="unit" th:text="${serie.unit}"></td>
<td class="date" th:text="${@seriesController.dateTimeFormat(serie.lastDate)}"></td>
</tr>
</table>
</div>
</body>
</html>