Pass URL Variables in Spring Controller at Runtime for Redirection Implementations

Viewed 34

I created the below frontend part for my Spring Boot Application. In this picture I've placed my cursor on the first News Link url and you can see at the bottom left corner that the link appears as it should be, just before I click it.

FrontEnd for Spring Boot App

When I click it, instead of

https://www.investing.com/news/cryptocurrency-news/ethereum-climbs-12-in-rally-2848020

I am being redirected to

https://www.investing.com/

From what I am thinking I have to firstly pass the URLs in my controller like this example and secondly to implement a redirection like this example. I tried the next method with no success. What am I doing wrong? I am new to Spring and I am struggling many days on this.

Failed Controller Attempt

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

Below my HTML Thymeleaf template,

HTML Thymeleaf

<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<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}"/>
                        </td>
                        <td> <img th:src="${rate.related_image}" alt="error displaying image"> </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>


</body>
</html>
0 Answers
Related