【发布时间】:2013-01-08 22:28:04
【问题描述】:
我找不到使用 Thymeleaf 从 URL 获取属性的任何解决方案。 例如,对于 URL:
somesite.com/login?error=true
我需要获取“错误”属性值。 如果有帮助的话,我也在使用 SpringMVC。
【问题讨论】:
标签: java spring templates thymeleaf
我找不到使用 Thymeleaf 从 URL 获取属性的任何解决方案。 例如,对于 URL:
somesite.com/login?error=true
我需要获取“错误”属性值。 如果有帮助的话,我也在使用 SpringMVC。
【问题讨论】:
标签: java spring templates thymeleaf
经过一番调查,我发现实际上是 Spring EL 问题。因此,使用空值检查的完整答案是:
<div id="errors" th:if="${(param.error != null) and (param.error[0] == 'true')}">
Input is incorrect
</div>
【讨论】:
在 thymeleaf 中访问请求参数的另一种方法是使用#httpServletRequest 实用程序对象,它可以直接访问javax.servlet.http.HttpServletRequest 对象。
空值检查的示例用法如下所示,
<div th:text="${#httpServletRequest.getParameter('error')}"
th:unless="${#httpServletRequest.getParameter('error') == null}">
Show some error msg
</div>
这与在 java 中执行 request.getParameter("error"); 相同。
【讨论】:
<a th:href="@{somesite.com/login(error = ${#httpServletRequest.getParameter('error')}"><a>
这可能有效。
【讨论】:
我试过了,它对我有用:
<div th:if="${param.error !=null}" class="col-xs-12 form-group">
</div>
【讨论】: