Thymeleaf: how to get URL attribute value

后端 未结 3 1477
天涯浪人
天涯浪人 2020-12-15 17:24

I can\'t find any solution for getting attribute from URL using Thymeleaf. For example, for URL:

somesite.com/login?error=true

I need to ge

相关标签:
3条回答
  • 2020-12-15 18:09

    Another way of accessing request parameters in thymeleaf is by using #httpServletRequest utility object which gives direct access to javax.servlet.http.HttpServletRequest object.

    An example usage with null checking looks like,

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

    This is same as doing request.getParameter("error"); in java.

    Source: Thymeleaf Docs

    0 讨论(0)
  • 2020-12-15 18:11

    After some investigation I found that it was Spring EL issue actually. So complete answer with null checking is:

    <div id="errors" th:if="${(param.error != null) and (param.error[0] == 'true')}">
        Input is incorrect
    </div>
    
    0 讨论(0)
  • 2020-12-15 18:12
    <a th:href="@{somesite.com/login(error = ${#httpServletRequest.getParameter('error')}"><a>
    

    This may work.

    0 讨论(0)
提交回复
热议问题