You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot整合Thymeleaf页面外部网站跳转异常问题

问题背景

基于Spring Boot开发的业务应用核心逻辑如下:从外部API拉取数据后持久化至数据库,再通过Thymeleaf模板引擎将数据渲染到前端HTML页面完成展示。目前除超链接跳转异常外,其余所有功能均运行正常。

数据库中设置了名为HEADLINE的字段,映射为String类型的实体属性,用于存储新闻标题内容;开发时将该字段值作为超链接显示文本,结合同表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,但点击超链接后无法跳转至数据库存储的完整目标地址:

  • 例如目标地址为https://www.investing.com/news/cryptocurrency-news/tether-liquidates-celsius-position-with-no-losses-to-stablecoin-issuer-2845626时,实际仅跳转至https://www.investing.com/域名根路径

页面浏览器展示效果如下图:
浏览器运行效果

已尝试的排查方案
  • 方案1:将a标签中th:href="${rate.news_link}"修改为th:href="@{${rate.news_link}}",问题未解决,URL仍被截断至域名根路径
  • 方案2:新增后端重定向接口,代码如下:
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public ModelAndView method(
            @RequestParam(name = "news_link", required = false) String news_link) {
        return new ModelAndView("redirect:" + news_link);
    }

调用该接口时出现跳转地址为http://localhost:8080/null的空URL异常,问题仍未解决,需定位该异常的产生原因并给出修复方案。


内容的提问来源于stack exchange,提问作者Alex_Pap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 02:18:03