Maxim Kolesnikov
Maxim Kolesnikov

Reputation: 5135

Thymeleaf: how to get URL attribute value

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 get "error" attribute value. Also I'm using SpringMVC, if it could be helpful.

Upvotes: 27

Views: 43445

Answers (4)

Abd Abughazaleh
Abd Abughazaleh

Reputation: 5515

I tried this and it's working for me :

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

Upvotes: 2

Maxim Kolesnikov
Maxim Kolesnikov

Reputation: 5135

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>

Upvotes: 47

Guwang
Guwang

Reputation: 11

<a th:href="@{somesite.com/login(error = ${#httpServletRequest.getParameter('error')}"><a>

This may work.

Upvotes: 1

Lucky
Lucky

Reputation: 17345

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

Upvotes: 7

Related Questions