我如何 return Java 中 RethinkDB 的数据列表 Spring/thymeleaf
How would I return a list of data from RethinkDB in Java for Spring/thymeleaf
所以我有一个 table(在 RethinkDB 中称为视频),我想有一个函数可以 return 列表(或其他东西)中的所有数据。因为我想要一个网页,其中包含我 table 中的所有视频,使用 thymeleaf(遍历列表)。
我一直在查看 RethinkDB 文档,并尝试了这些示例。
但主要问题始终是能够将数据转换为列表。
For example:
@GetMapping("/videos")
public String showVideos(Model model) {
System.out.println(Videos.getVideos(conn));
return "videos";
}
public static Table getVideos(Connection conn) {
Table videos = r.table("videos").run(conn);
return videos;
}
Results in:
nested exception is java.lang.ClassCastException: class com.rethinkdb.net.Cursor$DefaultCursor cannot be cast to class java.util.List (com.rethinkdb.net.Cursor$DefaultCursor is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')] with root cause
java.lang.ClassCastException: class com.rethinkdb.net.Cursor$DefaultCursor cannot be cast to class java.util.List (com.rethinkdb.net.Cursor$DefaultCursor is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')
When using this code:
Cursor cursor = r.table("videos").run(conn);
for (Object doc : cursor) {
System.out.println(doc);
}
我能够看到每个值。但是话又说回来,我不知道如何以 thymeleaf 能够迭代的方式实际放置它。
In the RethinkDB Documentation I've also found this:
try(Result<Object> result = r.table("users").run(conn)) {
for (Object doc : result) {
processResult(doc);
}
}
但我似乎找不到结果(我认为是 class)的来源。
我找到的解决方案:
@GetMapping("/videos")
public String showVideos(Model model) {
ArrayList<Videos> videosHTML = new ArrayList<Videos>();
Cursor<Videos> videos = r.table("videos").run(conn, Videos.class);
for(Videos vid : videos) {
videosHTML.add(vid);
}
model.addAttribute("videotitles", videosHTML);
return "videos";
}
<tr th:each="vid: ${videotitles}">
<td th:text="${vid.title}"></td>
<td th:text="${vid.description}"></td>
<td th:text="${vid.id}"></td>
<a th:href="'/video/'+${vid.id}"
href="/video.html"
class="buttonLook mediumButton">Watch Video</a>
<br>
</tr>
所以我有一个 table(在 RethinkDB 中称为视频),我想有一个函数可以 return 列表(或其他东西)中的所有数据。因为我想要一个网页,其中包含我 table 中的所有视频,使用 thymeleaf(遍历列表)。
我一直在查看 RethinkDB 文档,并尝试了这些示例。 但主要问题始终是能够将数据转换为列表。
For example:
@GetMapping("/videos")
public String showVideos(Model model) {
System.out.println(Videos.getVideos(conn));
return "videos";
}
public static Table getVideos(Connection conn) {
Table videos = r.table("videos").run(conn);
return videos;
}
Results in:
nested exception is java.lang.ClassCastException: class com.rethinkdb.net.Cursor$DefaultCursor cannot be cast to class java.util.List (com.rethinkdb.net.Cursor$DefaultCursor is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')] with root cause
java.lang.ClassCastException: class com.rethinkdb.net.Cursor$DefaultCursor cannot be cast to class java.util.List (com.rethinkdb.net.Cursor$DefaultCursor is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')
When using this code:
Cursor cursor = r.table("videos").run(conn);
for (Object doc : cursor) {
System.out.println(doc);
}
我能够看到每个值。但是话又说回来,我不知道如何以 thymeleaf 能够迭代的方式实际放置它。
In the RethinkDB Documentation I've also found this:
try(Result<Object> result = r.table("users").run(conn)) {
for (Object doc : result) {
processResult(doc);
}
}
但我似乎找不到结果(我认为是 class)的来源。
我找到的解决方案:
@GetMapping("/videos")
public String showVideos(Model model) {
ArrayList<Videos> videosHTML = new ArrayList<Videos>();
Cursor<Videos> videos = r.table("videos").run(conn, Videos.class);
for(Videos vid : videos) {
videosHTML.add(vid);
}
model.addAttribute("videotitles", videosHTML);
return "videos";
}
<tr th:each="vid: ${videotitles}">
<td th:text="${vid.title}"></td>
<td th:text="${vid.description}"></td>
<td th:text="${vid.id}"></td>
<a th:href="'/video/'+${vid.id}"
href="/video.html"
class="buttonLook mediumButton">Watch Video</a>
<br>
</tr>