Thymeleaf构造带变量的URL [英] Thymeleaf construct URL with variable

查看:619
本文介绍了Thymeleaf构造带变量的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中设置了以下代码变量:

I have the following code setting a variable in my controller:

model.set("type", type);

在百里香的视图中,我想构建一个带有动作网址的表单:

In the thymeleaf view I want to construct a form with action url:

/mycontroller/{type}

任何想法如何实现这一目标?我没有运气就读过百里叶文件。

Any ideas how to achieve this? I've read thymeleaf documentation with no luck.

推荐答案

as user482745在评论中建议(现已删除),我之前建议的字符串连接

As user482745 suggests in the comments (now deleted), the string concatenation I previously suggested

<form th:action="@{/mycontroller/} + ${type}">

在某些网络环境中会失败。

will fail in some web contexts.

Thymeleaf使用 LinkExpression 解析 @ {..} 表达式。在内部,它使用 HttpServletResponse的#encodeURL(字符串) 。它的javadoc状态

Thymeleaf uses LinkExpression that resolves the @{..} expression. Internally, this uses HttpServletResponse#encodeURL(String). Its javadoc states


对于健壮的会话跟踪,servlet发出的所有URL都应该通过此方法运行
。否则,URL重写不能与不支持cookie的
浏览器一起使用。

For robust session tracking, all URLs emitted by a servlet should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.

在会话跟踪的Web应用程序中通过URL完成,该部分将附加到 @ {..} $ {..} 。你不想要这个。

In web applications where the session tracking is done through the URL, that part will be appended to the string emitted for @{..} before the ${..} is appended. You don't want this.

相反,使用文档


您还可以包含以下形式的参数:路径变量
与普通参数类似,但在
中指定占位符您的URL路径:

You can also include parameters in the form of path variables similarly to normal parameters but specifying a placeholder inside your URL’s path:

<a th:href="@{/order/{id}/details(id=3,action='show_all')}">


所以你的例子看起来像

<form th:action="@{/mycontroller/{path}(path=${type})}"> //adding ending curly brace

这篇关于Thymeleaf构造带变量的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆