我有以下代码在我的控制器中设置变量:
model.set("type", type);
在 thymeleaf 视图中,我想构建一个带有操作 url 的表单:
/mycontroller/{type}
有什么想法如何实现这一目标?我读过百里香文档但没有运气。
As user482745 在评论中建议 https://stackoverflow.com/questions/14938344/thymeleaf-construct-url-with-variable#comment53400366_14938399(现已删除),我之前建议的字符串连接
<form th:action="@{/mycontroller/} + ${type}">
在某些网络环境中会失败。
百里香叶用途LinkExpression http://www.thymeleaf.org/apidocs/thymeleaf/2.1.4.RELEASE/org/thymeleaf/standard/expression/LinkExpression.html这解决了@{..}
表达。在内部,这使用HttpServletResponse#encodeURL(String) http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletResponse.html#encodeURL-java.lang.String-。它的 javadoc 指出
为了实现强大的会话跟踪,servlet 发出的所有 URL 都应该是
运行这个方法。否则,URL 重写不能与
不支持cookie的浏览器。
在通过 URL 完成会话跟踪的 Web 应用程序中,该部分将附加到发出的字符串中@{..}
之前${..}
已附加。你不想要这个。
相反,请按照建议使用路径变量文档 http://www.thymeleaf.org/doc/articles/standardurlsyntax.html#adding-parameters
您还可以以路径变量的形式包含参数
与普通参数类似,但在内部指定占位符
您的 URL 路径:
<a th:href="@{/order/{id}/details(id=3,action='show_all')}">
所以你的例子看起来像
<form th:action="@{/mycontroller/{path}(path=${type})}"> //adding ending curly brace
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)