I apologize in advance, I am new to Bootstrap modals in Thymeleaf, and I need help in order to show status messages after an update using a Bootstrap modal form.
I am writing an application in Spring Boot, using Thymeleaf for my view templates. I have a product page, showing a list of products in a table, and I have an update button for each product. The update button opens the update form in a bootstrap modal.
Once the form is submitted, the back end checks everything, and updates if there is no error. It then sends back a message, and passes it to the product view page.
Everything works fine, but my problem is that the message shows up in the url, and is not displayed on the product page.
In the product html page, the message is shown as follows:
<div th:if="${msg}" th:text="${msg}">message</div>
From the controller, the message is sent as an attribute for a redirect:
String msg = "Product updated";
redirectAttributes.addAttribute("msg", msg);
return "redirect:/product/" + product.getId();
The update form is in a modal that closes once the submit button is clicked. But when the modal closes, the msg appears in the url, and not on the product page.
How could I solve this, and display the message correctly, on the page, or in a pop up? is there a way to actually reload the page, so that it takes into consideration the new message and shows it properly?
Thank you very much for your help!
I found the issue: the message did not show properly because I used add attribute, and I should use addFlashAttribute.
It does not work with:
redirectAttributes.addAttribute("msg", msg);
It works with:
redirectAttributes.addFlashAttribute("msg", msg);
Based on this explanation, my understanding is that it makes sense to use FlashAttribute after a modal window, because FlashAttribute uses a map stored in the session, which would be updated after I close my modal, vs. addAttribute, which would need to reload the original page.
If this understanding is not correct, I will be grateful if someone can explain why in my case FlashAttribute works and not Attribute.