我有下面的代码设置在我的控制变量:Thymeleaf构造URL可变
model.set("type", type);
在thymeleaf观点我想构建具有操作URL形式:
/mycontroller/{type}
任何想法如何实现这一目标?我没有运气阅读过thymeleaf文档。
我有下面的代码设置在我的控制变量:Thymeleaf构造URL可变
model.set("type", type);
在thymeleaf观点我想构建具有操作URL形式:
/mycontroller/{type}
任何想法如何实现这一目标?我没有运气阅读过thymeleaf文档。
由于user482745 suggests in the comments(现已删除),字符串连接我曾建议
<form th:action="@{/mycontroller/} + ${type}">
会在一些网络环境中失败。
Thymeleaf使用LinkExpression
可解决@{..}
表达式。在内部,这使用HttpServletResponse#encodeURL(String)
。它的javadoc状态
对于健壮的会话跟踪,servlet发出的所有URL应该是 ,通过此方法运行。否则,URL重写不能用于不支持cookie的 浏览器。
在网络应用中的会话跟踪是通过URL来完成,该部分将被附加到${..}
之前@{..}
发出的字符串追加。你不想要这个。
相反,使用路径变量作为documentation
建议你也可以包括在路径变量的形式参数 类似于正常的参数,但指定内部 您的URL的路径的占位符:
<a th:href="@{/order/{id}/details(id=3,action='show_all')}">
所以你的例子看起来像
<form th:action="@{/mycontroller/{path}(path=${type})}"> //adding ending curly brace
如果你不想使用字符串连接(proposed by Sotirios),您可以在URL link使用expression preprocessing:
<form th:action="@{/mycontroller/__${type}__}">
我也认为这是比Sotirios提出的解决方案更好的解决方案。我有像'th:action =“@ {/ offers/__ $ {offer.id} __/changeStatus}”''的链接。 – sebast26 2014-08-21 06:41:35
你需要内部@置字符串{}。
<form th:action="@{'/mycontroller/' + ${type}}">
@ {}用于URL重写。部分URL重写正在跟踪会话。第一次用户请求的URL,应用服务器添加到url ;jsessionid=somehexvalue
并生成jsessionid的cookie。当客户端在下一个请求服务器发送cookie时知道客户端支持cookie。如果服务器知道客户端支持cookie,服务器将不会在URL中保留addind jsessionid。
我的首选方法是使用管道语法(|)进行文字替换。
<form th:action="@{|/mycontroller/${type}|}">
Thymeleaf路径变量语法是
<form th:action="@{/mycontroller/{pathParam}(pathParam=${type}}">
我更喜欢字面替换,因为它更清晰易于维护。对于简单的任务,thymeleaf语法太复杂。 – 2016-06-24 18:50:30
做字符串连接对我有用,但这非常难看。为什么不写'th:href =“@ {/ foo/bar/{obj.property}}”'?这对我不起作用。 – Rick 2017-01-05 09:16:46
Exception evaluating SpringEL expression: "businessId" (login:50)
我有同样的问题,并通过串concatination解决像的下方。
LoginController.java
@RequestMapping(value = "/login/{businessId}", method = RequestMethod.GET)
public ModelAndView get(HttpServletRequest request, @PathVariable String businessId) {
ModelAndView modelAndView = new ModelAndView("login");
modelAndView.addObject("businessId", businessId);
return modelAndView;
}
的login.html
<form role="form" th:action="@{/login} + '/'+ ${businessId}" th:method="post">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="E-mail" name="userName"
type="email"></input>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password"
name="password" type="password" value=""></input>
</div>
<div class="checkbox">
<label> <input name="remember" type="checkbox"
value="Remember Me"></input>Remember Me
</label>
</div>
<!-- Change this to a button or input when using this as a form -->
<button id="login" class="btn btn-lg btn-success btn-block" type="submit">Login</button>
</fieldset>
</form>
你需要什么,它是:
<a th:href="@{/mycontroller/{type}(type=${type})}">
文档:
很大的帮助是在这里: http://www.thymeleaf.org/doc/articles/standardurlsyntax.html。我用 从有:
您还可以在路径变量 类似于正常的参数,但指定内部 您的URL的路径的占位符的形式参数:
<a th:href="@{/order/{id}/details(id=3,action='show_all')}">
。 .. 更多:URL表达式如下:
<a th:href="@{/order/details(id=${order.id})}">
有一点要提的是,参数需要全部在链接表达式的末尾。 这是不正确的语法: '@ {/顺序/(编号)(ID = $ {anIdExpression})/细节/ {详细}(详细= $ {aDetailExpression)}' 但是,这是正确的: '@ {/ order/{id}/detail/{detail}(id = $ {anIdExpression},detail = $ {aDetailExpression}} 这个文档不是很清楚,所以我想我会我花费在试验和错误上的时间,节省了其他人。 – rdguam 2018-01-16 01:20:35
这对我不起作用。对于上面的例子,我将'{user}'直接渲染到URL中。例如,'First'给了我一个“http:// host/users/{user.first}”链接,并带有文本“Rick”。 – Rick 2017-01-05 09:14:19
@rick在'@ {...}'里面你没有指定占位符{user.first}'的值。如果我的Thymeleaf仍然正确,则需要'@ {/ users/{path}(path = $ {user.first})}'。 – 2017-01-05 22:06:50
谢谢,Sotirios,似乎是这样。我当然希望我可以在没有所有样板的情况下在路径中写入'$ {user.first}'(或其他)。我真的不明白为什么它不支持这种明显的用法。 – Rick 2017-01-05 23:04:34