【问题标题】:Thymeleaf: how to get URL attribute valueThymeleaf:如何获取 URL 属性值
【发布时间】:2013-01-08 22:28:04
【问题描述】:

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

somesite.com/login?error=true

我需要获取“错误”属性值。 如果有帮助的话,我也在使用 SpringMVC。

【问题讨论】:

    标签: java spring templates thymeleaf


    【解决方案1】:

    经过一番调查,我发现实际上是 Spring EL 问题。因此,使用空值检查的完整答案是:

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

    【讨论】:

    • 你今天帮我节省了几个小时 :)
    【解决方案2】:

    在 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"); 相同。

    来源:Thymeleaf Docs

    【讨论】:

      【解决方案3】:
      <a th:href="@{somesite.com/login(error = ${#httpServletRequest.getParameter('error')}"><a>
      

      这可能有效。

      【讨论】:

        【解决方案4】:

        我试过了,它对我有用:

        <div th:if="${param.error !=null}" class="col-xs-12 form-group">
               
        </div>
        

        【讨论】:

          猜你喜欢
          • 2017-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多