Skip to content

Commit 46a3545

Browse files
committed
simple thymeleaf page
1 parent 94671b8 commit 46a3545

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@
128128
<version>7.4.1</version>
129129
<scope>test</scope>
130130
</dependency>
131+
<dependency>
132+
<groupId>io.micronaut.views</groupId>
133+
<artifactId>micronaut-views-thymeleaf</artifactId>
134+
</dependency>
131135
</dependencies>
132136

133137
<build>

src/main/java/com/example/controller/api/GreetController.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
package com.example.controller.api;
22

3+
import com.example.service.BookService;
34
import com.example.service.GreetingService;
5+
import io.micronaut.core.util.CollectionUtils;
46
import io.micronaut.core.version.annotation.Version;
57
import io.micronaut.http.HttpRequest;
68
import io.micronaut.http.HttpResponse;
79
import io.micronaut.http.MediaType;
810
import io.micronaut.http.annotation.Controller;
911
import io.micronaut.http.annotation.Get;
1012
import io.micronaut.http.annotation.Produces;
13+
import io.micronaut.views.View;
1114
import jakarta.inject.Inject;
1215

16+
17+
import java.text.SimpleDateFormat;
18+
import java.util.Date;
19+
import java.util.Map;
20+
1321
@Controller
1422
public class GreetController {
1523

1624
@Inject
17-
private GreetingService greetingService;
25+
private final GreetingService greetingService;
26+
27+
@Inject
28+
private final BookService bookService;
29+
30+
public GreetController(GreetingService greetingService, BookService bookService) {
31+
this.greetingService = greetingService;
32+
this.bookService = bookService;
33+
}
1834

1935
@Version("1.0")
2036
@Get("/{name}")
@@ -56,4 +72,13 @@ public String readMe() {
5672
</html>
5773
""";
5874
}
75+
76+
@View("books/books")
77+
@Get("/test")
78+
public Map getHome() {
79+
Date date = new Date();
80+
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
81+
82+
return CollectionUtils.mapOf( "books", bookService.findAll());
83+
}
5984
}

src/main/java/com/example/generall/URlConstants.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ public URlConstants() throws IllegalAccessException {
66
throw new IllegalAccessException("it is just for constants");
77
}
88

9-
public static final String BOOKS = "books";
9+
public static final String API = "api/";
10+
public static final String BOOKS = API + "books";
1011
public static final String COUNT = "count";
1112

12-
public static final String AUTHORS = "authors";
13+
public static final String AUTHORS = API + "authors";
1314
public static final String VERSION_1_1 = "1.1";
1415
public static final String ID = "{id}";
1516
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org" lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Library</title>
6+
<style>
7+
td, th {
8+
padding-left: 35px;
9+
padding-bottom: 10px;
10+
text-align: left;
11+
}
12+
table {
13+
border-collapse: collapse;
14+
}
15+
tr {
16+
border: solid;
17+
border-width: 1px 0;
18+
}
19+
h1 {
20+
text-align: center;
21+
}
22+
</style>
23+
</head>
24+
<body>
25+
26+
<h1>My Library &ndash; List of books</h1>
27+
28+
<table>
29+
<thead>
30+
<tr>
31+
<th> ID</th>
32+
<th> Title</th>
33+
<th> Pages</th>
34+
<th> Year</th>
35+
<th> Author</th>
36+
</tr>
37+
</thead>
38+
<tbody>
39+
<tr th:each="book : *{books}">
40+
<td th:text="*{book.id}"></td>
41+
<td th:text="*{book.title}"></td>
42+
<td th:text="*{book.pages}"></td>
43+
<td th:text="*{book.year}"></td>
44+
<td th:text="*{book.author.name}"></td>
45+
</tr>
46+
</tbody>
47+
</table>
48+
</body>
49+
</html>

0 commit comments

Comments
 (0)