title: How to create a dynamic link with Thymeleaf and Spring Boot?
body: <p>so I've been making a "website" hosted on my computer with Thymeleaf and Spring Boot, and I have a basic, insecure login that I made for fun. What I'm currently trying to do is to make a link that brings you back to a previous page based, which is sent in as a String through the model.</p>
  
  <p>Here are a few snippets:
  In the controller: </p>
  
  <pre><code>@RequestMapping("/")
  public String index(Model model) {
      return ifLoggedIn(model, "home", "");
  }
  
  private String ifLoggedIn (Model model, String location, String returnTo) {
      if (Login.loggedIn.getOrDefault(Login.IP(), Boolean.FALSE)) {
          return location;
      } else {
          model.addAttribute("login", new Login());
          model.addAttribute("return_page", returnTo);
          return "login";
      }
  }
  </code></pre>
  
  <p>In the loggedin.html file (After you log in successfully):</p>
  
  <pre><code>&lt;body&gt;
      &lt;a th:href="'/' + ${return_page}"&gt;Return to previous location&lt;/a&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I first change the line in the loggedin.html file to follow what Pau recommended:</p>
  
  <pre><code>&lt;a th:href="@{'/' + ${return}}"&gt;Return to previous location&lt;/a&gt;
  </code></pre>
  
  <p>But I kept getting redirected to server.local/null, so I tried removing the <code>'/' +</code> and having the / inside the ifLoggedIn parameters, so it would be <code>return ifLoggedIn(model, "home", "/"</code>, but that also kept sending me to server.local/null. Now, I just changed the line in the loggedin.html file to the following:</p>
  
  <pre><code>&lt;a th:if="(${return_page} != null)" th:with="return=(${return_page})" th:href="@{${return}}"&gt;Return to previous location&lt;/a&gt; 
  </code></pre>
  
  <p>Now it just disappears, meaning that ${return_page} is equal to null. Looking back at the controller I don't understand why it returns null. Another thing, I am redirecting back to other pages as well, with one line saying <code>return ifLoggedIn(model, "messages", "/messages"</code>, but even with those I still get sent to server.local/null.</p>