我试图实现this教程,但我不明白message/list是什么意思.我创建了一个Spring Boot应用程序,它使用另一个API,以JSON格式获取数据,并将其存储在PostgreSQL数据库中.

我数据库中的一列是来自该API的URL.我使用Thymeleaf在HTML的前端显示它们.我的目标是创建一个URL前端,当用户单击它们时,将它们重定向到我的应用程序之外的适当网站.

我必须同时将从Thymeleaf到控制器的数据传输和从控制器到应用程序外部的重定向结合起来.

有没有可能这样做?下面我向你们展示了我的前端代码,下面是我试图实现的控制器,但没有成功,因为它给了我.

java.lang.IllegalArgumentException: Model has no value for key 'rate.news_link'

FrontEnd

<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:c="http://www.w3.org/1999/XSL/Transform"
      xmlns:https="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>Spring Boot Thymeleaf Hello World Example</title>

    <link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}"/>
    <link rel="stylesheet" th:href="@{/styles/db.css}"/>
    <script type="text/javascript" th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>


</head>

<body>

<ul>
    <li><a href="http://localhost:8080/">Home</a></li>
    <li><a href="https://github.com/Andreas-Kreouzos" target="_blank">Github</a></li>
</ul>

<main role="main" class="container">

    <div class="starter-template">
        <h1>Cryptocurrency News</h1>
    </div>

</main>
    <!--Display DB contents on HTML and Thymeleaf-->

    <div class="container text-center" id="tasksDiv">
        <hr>
        <div class="table-responsive">
            <table class="table table-striped table-bordered">
                <thead>
                <tr>
                    <th>News Provider Name</th>
                    <th>News Link</th>
                    <th>Related Image</th>
                </tr>
                </thead>
                <tbody>
                    <tr th:each="rate: ${rates}">
                        <td th:text="${rate.news_provider_name}"> </td>
                        <td>
                            <a
                                    href="#"
                                    th:href="${rate.news_link}"
                                    th:text="${rate.HEADLINE}">
                            </a>
                        </td>
                        <td> <img th:src="${rate.related_image}" alt="error displaying image"> </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>


</body>
</html>

Controller

    @RequestMapping("/")
    public String redirect() {
        return "redirect:/${rate.news_link}";
    }

推荐答案

首先,您没有传递任何变量,因此无法使用它们.下面的教程使用的是常量值.

Java相关问答推荐

如何将一些命令写入Chrome控制台,然后使用Java将输出存储在selenium中

判断大写字符串的Java方法总是返回假

使用ExecutorService时在ThreadFactory中触发自定义newThread函数

当切换javaFX场景时,stage的大小正在Minimize

try 使用Java 9或更高版本对特殊对象图进行解析时出现NullPointerException

替换com. sun. jndi. dns. DnsContextFactory Wildfly23 JDK 17

@ IdClass with @ Inheritance(策略= InheritanceType. SINGLE_TABLE)

为什么我要创建一个单独的互斥体/锁对象?

CompleteableFuture是否运行在不同的内核上?

GSON期间的Java类型擦除

Spring和可编辑";where";@Query

如何在 spring 数据的MongoDB派生查询方法中使用$EXISTS

如何将Pane的图像快照保存为BMP?

无法使用Freemarker从XML中读取重复的标记值

如何配置空手道以使用FeignClient或RestTemplate代替ApacheHttpClient

如果第一位数字和最后一位数字相差超过一位,您将如何获得随机数?

向Java进程发送`kill-11`会引发NullPointerException吗?

在具有Quarkus Panache的PostgreSQL中将JSON数据存储为JSONB时,会将其存储为转义字符串

验证没有';t work on Hibernate Entity';s字段

在数组中查找素数时出现逻辑错误