Issue
I am trying to implement this tutorial but I don't understand what message/list
means. I've created a Spring Boot app that consumes another API, fetches it's data as JSON format and stores them in PostgreSQL database.
One of the columns in my DB are URL's that came from that API. I am displaying them at frontend in HTML using Thymeleaf. My goal is to create a frontend of URLs that when users click them, will redirect them at the appropriate website outside of my application.
I've to combine the data transfer from Thymeleaf back to controller and redirection from the controller to the outside of my application at the same time.
Is there any possibility to do this? Below I am showing you my frontend code and below that the controller that I tried to implement, with no success since it's giving me.
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}";
}
Solution
First you are not passing any variables so you cannot use them the tutorial you are following is using constant values.
If you want to use variables you can do that by taking input using @PathVariable
or @RequestParam
depending on your need or choice.
If you don't know spring boot or learning it, I recommend you first learn spring boot web(to make the rest APIs) and then move to Spring MVC.
Answered By - gaurav jha
Answer Checked By - Robin (JavaFixing Admin)