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.
Here are a few snippets: In the controller:
@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";
}
}
In the loggedin.html file (After you log in successfully):
<body>
<a th:href="'/' + ${return_page}">Return to previous location</a>
</body>
EDIT:
I first change the line in the loggedin.html file to follow what Pau recommended:
<a th:href="@{'/' + ${return}}">Return to previous location</a>
But I kept getting redirected to server.local/null, so I tried removing the '/' +
and having the / inside the ifLoggedIn parameters, so it would be return ifLoggedIn(model, "home", "/"
, but that also kept sending me to server.local/null. Now, I just changed the line in the loggedin.html file to the following:
<a th:if="(${return_page} != null)" th:with="return=(${return_page})" th:href="@{${return}}">Return to previous location</a>
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 return ifLoggedIn(model, "messages", "/messages"
, but even with those I still get sent to server.local/null.
While Thymeleaf is more of a template engine for server-side application development. But Thymeleaf's popularity is on a steady rise. The developer community is slowly moving away from 'once a common' MVC framework for Javascript-based development.
It provides full integration with Spring Framework. It applies a set of transformations to template files in order to display data or text produced by the application. It is appropriate for serving XHTML/HTML5 in web applications. The goal of Thymeleaf is to provide a stylish and well-formed way of creating templates.
You need to use link expressions using @{...}
which is a type of Thymeleaf Standard Epression
. In the official doc you can see a lot of examples using expressions inside your link expressions in section 9. Using expressions in URLs
:
No problem! Every URL parameter value is in fact an expression, so you can easily substitute your literals with any other expressions, including i18n, conditionals…:
....
the URL base itself can be specified as an expression, for example a variable expression:
<a th:href="@{${detailsURL}(id=${order.id})}">
Furthermore, in the tutorial there is a expressions which is similar as yours, in the section 4.4 Link URLs:
URL bases can also be the result of evaluating another expression:
...
<a th:href="@{'/details/'+${user.login}(orderId=${o.id})}">view</a>
So in your case, it would be like next:
<body>
<a th:href="@{'/' + ${return_page}}">Return to previous location</a>
</body>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With