Thymeleaf:如何获取URL属性值

Max*_*kov 19 java spring templates thymeleaf

我找不到使用Thymeleaf从URL获取属性的任何解决方案.例如,对于URL:

somesite.com/login?error=true
Run Code Online (Sandbox Code Playgroud)

我需要获得"错误"属性值.我也在使用SpringMVC,如果它可能有用.

Max*_*kov 33

经过一番调查,我发现实际上是Spring EL问题.所以null检查的完整答案是:

<div id="errors" th:if="${(param.error != null) and (param.error[0] == 'true')}">
    Input is incorrect
</div>
Run Code Online (Sandbox Code Playgroud)

  • 你今天救了我几个小时:) (2认同)

Luc*_*cky 7

在 thymeleaf 中访问请求参数的另一种方法是使用#httpServletRequest实​​用程序对象,它可以直接访问javax.servlet.http.HttpServletRequest对象。

空检查的示例用法如下:

<div th:text="${#httpServletRequest.getParameter('error')}" 
     th:unless="${#httpServletRequest.getParameter('error') == null}">
    Show some error msg
</div>
Run Code Online (Sandbox Code Playgroud)

这与 java 中的操作相同request.getParameter("error");

来源:Thymeleaf 文档