Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

In this short tutorial, we'll shed light on how to get URL attributes in the Thymeleaf views.

2. How to Get URL Parameter Attributes

Accessing URL attributes, or what we call request parameters, can be easily done in Thymleaf views using one of two special Thymleaf objects. The first way is to use the param object, and the second way is to use the #request object.

For demonstration purposes, let's consider a URL that holds one parameter, query:


2.1. Using the param Object

First, let's see how to use the param object to access the URL attribute “query”:

<div th:if="${param.query != null}">
    <p th:text="${param.query }"></p>

In the above example, if the parameter “query” isn't null, the value of “query” will be shown. Also, we should note that URL attributes can be multivalued. Let's see an example URL with a multivalued attribute:


In this case, we can access the values separately using brackets syntax:

<div th:if="${param.query != null}">
    <p th:text="${param.query[0] + ' ' + param.query[1]}" th:unless="${param.query == null}"></p>

2.2. Using the request Object

Next, let's see the second way to access URL attributes. We can use the special #request object that gives you direct access to the javax.servlet.http.HttpServletRequest object, which breaks a request down into parsed elements such as query attributes and headers.

Let's see how to use the #request object in a Thymleaf view:

<div th:if="${#request.getParameter('query') != null}">
    <p th:text="${#request.getParameter('query')}" th:unless="${#request.getParameter('query') == null}"></p>

In the above example, we used the special function getParameter(‘query') offered by the #request object. This method returns the value of a request parameter as a String, or null if the parameter does not exist.

3. Conclusion

In this quick article, we explained how to get URL attributes in the Thymeleaf views using param and #request objects. As always, the code snippets are available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Comments are closed on this article!