title: Thymeleaf + Spring MVC Post/Redirect/Get
body: <p>I'm doing a Post/Redirect/Get thingy, using thymeleaf and Spring MVC and it works fine. Apart from when I get to the page doing the GET and do a refresh, the ModelAttribute is reset. Here's a snippet of something similar I'm doing.</p>
  
  <pre class="lang-java prettyprint-override"><code>@RequestMapping("/site")
  public class SiteController {
      @RequestMapping(values = {"", "/"}, method = RequestMethod.GET)
      public String index(@ModelAttribute("form") Form form, Model model) {
          return "/site/index";
      }
  
      @RequestMapping(values = {"/postit"}, method = RequestMethod.POST)
      public String indexPoster(@ModelAttribute("form") Form form, Model model, RedirectAttributes redirectAttributes) {
          redirectAttributes.addFlashAttribute("form", form);
          return "redirect:/site/result";
      }
  
      @RequestMapping(values = {"/result"}, method = RequestMethod.GET)
      public String indexResult(@ModelAttribute("form") Form form, Model model) {
          // This will print the form, but if I do a page reload, the form properties will be reset.
          System.out.println(form);
          return "/site/result";
      }
  }
  </code></pre>
  
  <p>My <code>Form</code> object is just a simple java bean with one String property called <code>name</code>, obviously my real form has a buttload of properties, but this is for simplicity in the question.</p>
  
  <p>On my html page for /site/index I have a simple form using <code>th:object="${form}"</code> and <code>th:field="*{name}"</code> that does a post to <code>/postit</code>. And on my /site/result I just output the name entered in the form. (so it's sort of a Hello World at the moment :) ).</p>
  
  <p>This flow works for me, but if I hit refresh after the /site/result has been loaded, the form variable in <code>indexResult</code> gets reset.</p>
  
  <p>I've tried using</p>
  
  <pre class="lang-java prettyprint-override"><code>private Form form;
  @ModelAttribute("form")
  public Form getForm() {
      if (this.form == null) {
          this.form = new Form();
      }
      return this.form;
  }
  </code></pre>
  
  <p>on the class level, but it feels "hacky". And if I navigate away from the page, do other stuff and then return the old data will still be there.</p>
  
  <p>In this project we are refraining (as far as we can) to use the session to store data.</p>