Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dynamic link with Thymeleaf and Spring Boot?

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.

like image 504
Bennett D Avatar asked Jun 25 '17 04:06

Bennett D


People also ask

Is Thymeleaf still popular?

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.

Is Thymeleaf fully integrated with Spring?

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.


1 Answers

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:

  • http://www.thymeleaf.org/doc/articles/standardurlsyntax.html

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>
like image 72
Pau Avatar answered Oct 01 '22 19:10

Pau