Very simple question, but I can't find answer on it. My task is to show answer of my integral functions in Thymeleaf. I find a lot of information that i should us RedirectAttributes in spring controller, which I did. This is mine spring controller with two endpoints.
@GetMapping(value = "/")
public String getMainPage(Model model) {
return "index";
}
@PostMapping("/integrate")
public String calculateIntegral(@RequestParam("function") String function, @RequestParam("numFrom") double numFrom,
@RequestParam("numTo") double numTo, @RequestParam("step") double step,
RedirectAttributes redirectAttributes) {
double rectangleResult = calculatorService.integrateRectangleMethod(numFrom, numTo, step, x -> 2);
double trapezoidalResult = calculatorService.integrateTrapezoidalMethod(numFrom, numTo, step, x -> 2);
log.info(String.valueOf(rectangleResult));
log.info(String.valueOf(trapezoidalResult));
redirectAttributes.addFlashAttribute("rectangleResult", rectangleResult);
redirectAttributes.addFlashAttribute("trapezoidalResult", trapezoidalResult);
return "redirect:/";
}
and my template where I want to display my answers
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Integral Calculator</title>
<link href="/css/main.css" th:href="@{/css/main.css}" rel="stylesheet"/>
</head>
<header>Integral Calculator</header>
<body>
<main>
<div class="container">
<form action="/integrate" method="post">
<section class="container">
<label><span class="label-text">Enter integrand f(x): </span><input name="function" class="input-cb" type="text"></label>
<fieldset>
<legend>Enter range:</legend>
<label><span class="label-text">From: </span><input name="numFrom" class="input-range-from" type="number"></label>
<label><span class="label-text">To: </span><input name="numTo" class="input-range-to" type="number"></label>
</fieldset>
<label><span class="label-text">Enter step: </span><input name="step" class="input-step" type="number" value="0.01"
step="0.1" min="0"></label>
<p class="error-message is-hidden"></p>
<button class="btn" type="submit">
Calculate
</button>
<br>
<div class="results is-hidden">
<p class="result">Results:</p>
<p class="result-rect" th:text="${rectangleResult}"></p>
<p class="result-trap" th:text="${trapezoidalResult}"></p>
</div>
</section>
</form>
<section class="chart-container">
<p class="chart-info is-hidden">The chart is only available for real number results</p>
<canvas id="myChart"></canvas>
</section>
</div>
</main>
</body>
When I push button in html page, my controller correctly show in logs answers, but they do not appear on my page. And one more, in template my rectangleResult and trapezoidalResult are underlined in red, which means that Thymeleaf does not see them, I do not understand why. P.S. I found a theme in stackoverflow How to set a Flash Message in Spring Boot with Thymeleaf, but as you can see I did the same as what was advised in this thread but it doesn't work for me