我创建了一个 Spring Boot 应用程序,在其中我从外部 API 获取数据,将它们存储在数据库中,然后使用 Thymeleaf 在 HTML 中将它们显示在前端。除了一件事之外,一切都工作得很好。
db 中的列之一的名称为“HEADLINE”,它映射为字符串变量并包含新闻标题。我将此列值用作 Spring Boot 应用程序中的文本超链接,并与存储在同一数据库(“news_link”列)中的 URL 相结合,以重定向到它们的源。下面我展示我的前端代码。
前端页面
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:c="http://www.w3.org/1999/XSL/Transform">
<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">Github</a></li>
</ul>
<main role="main" class="container">
<div class="starter-template">
<h1>IoT Application</h1>
</div>
</main>
<!--Display DB contents on HTML and Thymeleaf-->
<div class="container text-center" id="tasksDiv">
<h3>Database Contents</h3>
<hr>
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>news_id</th>
<th>news_provider_name</th>
<th>HEADLINE</th>
<th>news_link</th>
<th>related_image</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
<tr th:each="rate: ${rates}">
<td th:text="${rate.news_id}"> </td>
<td th:text="${rate.news_provider_name}"> </td>
<td th:text="${rate.HEADLINE}"> </td>
<td> <a th:href="${rate.news_link}"> <p th:text="${rate.HEADLINE}"> </a> </td>
<td> <img th:src="${rate.related_image}" alt="error displaying image"> </td>
<td th:text="${rate.timestamp}"> </td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
当我运行我的应用程序时,一切都显示得很好,但是一旦我按下文本超链接,我就会被重定向到主页,而不是我存储在数据库中的给定 URL。下面,我向您展示了我的打印屏幕,当我在浏览器中将光标悬停在 URL 上方时,就在按下 URL 之前,您肯定可以看到 URL 已被正确获取和分配。但一旦他们受到压力,我就会被重定向here https://www.investing.com/, 代替here https://www.investing.com/news/cryptocurrency-news/tether-liquidates-celsius-position-with-no-losses-to-stablecoin-issuer-2845626.
关于我做错了什么有什么想法吗?
UPDATE
我试图改变th:href="${rate.news_link}
to th:href="@{${rate.news_link}}
但仍然没有任何效果。例如,如果我的初始 URL 是
https://www.investing.com/news/cryptocurrency-news/tether-liquidates-celsius-position-with-no-losses-to-stablecoin-issuer-2845626
被砍掉https://www.investing.com/
UPDATE 2
我创建了以下控制器,但我得到空 URL ->http://localhost:8080/null
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method(
@RequestParam(name = "news_link", required = false) String news_link) {
return new ModelAndView("redirect:" + news_link);
}