【发布时间】:2013-02-03 00:06:02
【问题描述】:
我有以下代码在我的控制器中设置一个变量:
model.set("type", type);
在 thymeleaf 视图中,我想构造一个带有操作 url 的表单:
/mycontroller/{type}
任何想法如何实现这一目标?我已经阅读了 thymeleaf 文档,但没有运气。
【问题讨论】:
我有以下代码在我的控制器中设置一个变量:
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 完成会话跟踪的 Web 应用程序中,该部分将在附加 ${..} 之前附加到为 @{..} 发出的字符串中。你不想要这个。
改为使用documentation中建议的路径变量
还可以包含路径变量形式的参数 类似于普通参数,但在内部指定一个占位符 您的网址路径:
<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}__}">
【讨论】:
th:action="@{/offers/__${offer.id}__/changeStatus}"之类的链接。
您需要在@{} 中连接字符串。
<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}}">
【讨论】:
th:href="@{/foo/bar/{obj.property}}" 不起作用?这对我不起作用。
<form th:action="@{/mycontroller/{pathParam}(pathParam=${type})}">
你需要的是:
<a th:href="@{/mycontroller/{type}(type=${type})}">
文档:
这里有很大的帮助: http://www.thymeleaf.org/doc/articles/standardurlsyntax.html。我用了什么 从那里开始:
还可以包含路径变量形式的参数 类似于普通参数,但在内部指定一个占位符 您的网址路径:
<a th:href="@{/order/{id}/details(id=3,action='show_all')}">... 更重要的是:一个 URL 表达式,例如:
<a th:href="@{/order/details(id=${order.id})}">
【讨论】:
@{/order/{id}(id=${anIdExpression})/detail/{detail}(detail=${aDetailExpression)} 但这是正确的:@{/order/{id}/detail/{detail}(id=${anIdExpression}, detail=${aDetailExpression)} 文档在这一点上不是很清楚,所以我想我会节省其他人花费在反复试验上的时间。
Exception evaluating SpringEL expression: "businessId" (login:50)
我有同样的问题,通过下面的字符串连接解决。
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>
【讨论】: