Spring-Boot: redirecting and updating the model and page

I have a spring-boot application, with thememleaf. I am reloading the page and redirecting it to the same page, so I expect the page elements to be refreshed:

@GetMapping("/suggested-events/vote/{eventId}") public String voteForEvents(Model model, @PathVariable("eventId") Long eventId, @RequestParam(value = "message", required = false) String message ) { log.info("The message is: "+message); SuggestedEvent event = suggestedEventService.findSuggestedEventById(eventId); ArrayList<SuggestedEvent> events = suggestedEventService.findSuggestedEventsByArea(event.getArea()); model.addAttribute("mainEvent",event); model.addAttribute("events",events); model.addAttribute("message",message); return "/suggested-event/vote"; } 

and when the button is clicked in the view, it invokes the following post method:

 @PostMapping("/suggested-events/vote") public String voteForASuggestedEvent(RedirectAttributes redirectAttributes){ log.info("You have made a vote"); redirectAttributes.addAttribute("message", "Success"); return "redirect:/suggested-events/vote/1"; } 

This second controller method performs an operation a, makes a message and redirects it to the first method. Thus, it is successfully redirected to the first method and writes

 log.info("The message is: "+message); 

but it doesn’t refresh my page, and I don’t get the message as a model ?

When I redirect to the first method, I expect it to add message to my models:

 model.addAttribute("message",message); 

But it is not added to my page

+7
redirect spring spring-mvc thymeleaf
source share
2 answers

There are many ways to redirect a page in Spring, but make sure that if the model attribute turned off the message that it was correctly passed to FrontEnd or passed it as a parameter to another handler, you can see this document: http://javainsimpleway.com/spring-mvc-redirecting- model-attributes-from-one-controller-to-other-controller / , hope this is useful !!

0
source share

and when the button is pressed in the view, it calls the following Method entry:

This trigger seems to be using AJAX, not the submit form. This will be consistent with the symptoms you describe.

If you POST before /suggested-events/vote using AJAX, the server will return 302 and the browser will follow it. However, the answer for this 302 is still the result of an AJAX call. You have access to it in your callback, but the browser is not going to display it for you.

but does not refresh my page

If 302 does not cause a re-rendering of your page, it also indicates that you are using AJAX.

If you actually use the submit form instead, the browser will re-render using the markup returned by the successful redirect.

This can be verified using the following two buttons in vote.html :

  <form action="http://localhost:8080/suggested-events/vote" method="POST"> <input type="submit" text="Submit" /> </form> <button onclick="postmessage();" >Button</button> <script> function postmessage() { $.ajax({ method: 'POST', data: {}, url: 'http://localhost:8080/suggested-events/vote' }); } </script> 

The first button will work as expected, and the second button corresponds to the symptoms you described.

If you already using the form, update the question (or, even better, the entire Thymeleaf template).

0
source share

All Articles