如何使用 Thymeleaf 和 Spring Boot 创建动态链接? [英] How to create a dynamic link with Thymeleaf and Spring Boot?
问题描述
所以我一直在用 Thymeleaf 和 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";
}
}
在loggedin.html文件中(登录成功后):
In the loggedin.html file (After you log in successfully):
<body>
<a th:href="'/' + ${return_page}">Return to previous location</a>
</body>
我首先更改了 login.html 文件中的行以遵循 Pau 的建议:
I first change the line in the loggedin.html file to follow what Pau recommended:
<a th:href="@{'/' + ${return}}">Return to previous location</a>
但我一直被重定向到 server.local/null,所以我尝试删除 '/' +
并将/放在 ifLoggedIn 参数中,所以它是 return ifLoggedIn(模型,home",/"
,但这也一直将我发送到 server.local/null.现在,我只是将 login.html 文件中的行更改为以下内容:
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>
现在它消失了,这意味着 ${return_page} 等于 null.回顾控制器,我不明白为什么它返回 null.另一件事,我也重定向回其他页面,其中一行说 return ifLoggedIn(model, "messages", "/messages"
,但即使使用那些我仍然会被发送到 server.local/null.
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.
推荐答案
您需要使用 @{...}
的链接表达式,这是一种 Thymeleaf Standard Epression
代码>.在官方文档中,您可以在 9 部分的链接表达式中看到很多使用表达式的示例.在 URL 中使用表达式
:
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
:
没问题!每个 URL 参数值实际上都是一个表达式,因此您可以轻松地将您的文字替换为任何其他表达式,包括 i18n、条件……:
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…:
....
可以将 URL 库本身指定为表达式,例如变量表达式:
the URL base itself can be specified as an expression, for example a variable expression:
此外,在教程中,在 4.4 链接网址:
Furthermore, in the tutorial there is a expressions which is similar as yours, in the section 4.4 Link URLs:
URL bases 也可以是对另一个表达式求值的结果:
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>
这篇关于如何使用 Thymeleaf 和 Spring Boot 创建动态链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!