相关文章推荐
睿智的熊猫  ·  双男%E - 图片搜索·  1 年前    · 
细心的台灯  ·  成語檢索 [一] - ...·  1 年前    · 
Partner – Trifork – NPI EA (cat = Spring Boot)
announcement - icon

Navigating the complexities of Spring can be difficult, even for seasoned developers.

If you need direct, practical help and guidance with your own Spring work , Trifork's CTO, Joris Kuipers, is running a closed-door call.

It's free, but it's limited to only 3 seats , so if you need it, I would join quickly and be sure to attend:

CTO Spring Open Office Hour Session - Technical Guidance

With more than 15 years of leading custom software development projects involving Spring, Joris has gained a lot of real-world experience, and this call is about sharing and helping the community.

Enjoy.

Partner – Jmix Haulmont – NPI EA (cat= Architecture)
announcement - icon

Building or modernizing a Java enterprise web app has always been a long process, historically. Not even remotely quick.

That's the main goal of Jmix is to make the process quick without losing flexibility - with the open-source RAD platform enabling fast development of business applications.

Critically, it has very minimal impact on your server's performance, with most of the profiling work done separately - so it needs no server changes, agents or separate services.

Simply put, a single Java or Kotlin developer can now quickly implement an entire modular feature, from DB schema, data model, fine-grained access control, business logic, BPM, all the way to the UI.

Jmix supports both developer experiences – visual tools and coding , and a host of super useful plugins as well:

>> Try out Jmix

Partner – AEGIK AB – NPI EA (tag = SQL)
announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only , so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server's performance, with most of the profiling work done separately - so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server , hit the record button, and you'll have results within minutes:

out the Profiler

Partner – CAST AI – NPI EA (tag = kubernetes)
announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

Kubernetes cost monitoring

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we're going to have a look at the different types of conditionals available in Thymeleaf.

For a quick introduction to Thymeleaf, please refer to this article .

2. Maven Dependencies

Let's start with the Maven dependencies that are required to use Thymeleaf along with Spring:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>

For other Spring releases, we should use the matching thymeleaf-springX library, where X stands for Spring version. Please also note that Spring 5 is supported by Thymeleaf starting with 3.0.8.RELEASE .

The latest versions of required dependencies can be found here .

3. Thymeleaf Conditionals

We have to distinguish between conditionals that allow us to render text within an HTML element depending on a condition and those that control the instantiation of an HTML element itself.

Let's define our Teacher model class that we'll use throughout this article:

public class Teacher implements Serializable {
    private String gender;
    private boolean isActive;
    private List<String> courses = new ArrayList<>();
    private String additionalSkills;

3.1. Elvis Operator

The Elvis operator ?: lets us render text within an HTML element depending on the current state of a variable.

We can use default expressions to provide a default text if a variable is null :

<td th:text="${teacher.additionalSkills} ?: 'UNKNOWN'" />

Here we want to display the content of the teacher.additionalSkills variable if it is defined, and we want the text “ UNKNOWN ” to be rendered otherwise.

It's also possible to display arbitrary text depending on a boolean expression:

<td th:text="${teacher.active} ? 'ACTIVE' : 'RETIRED'" />

We can query a simple boolean variable as in the previous example, but string comparisons and range checks are possible as well.

The following comparators and their textual representations are supported: > (gt) , >= (ge) , < (lt) , <= (le) , == (eq) and != (ne) .

3.2. If – Unless

The th:if and th:unless attributes allow us to render an HTML element depending on a provided condition:

<span th:if="${teacher.gender == 'F'}">Female</span> <span th:unless="${teacher.gender == 'F'}">Male</span>

If the content of the teacher.gender variable equals to an F , the span element with the value Female is rendered. Otherwise, the element with Male is rendered.

Such a setup is comparable to an if-else clause present in most programming languages.

3.3. Switch – Case

If there are more than two possible results of an expression, we can use the th:switch and th:case attributes for the conditional rendering of the HTML elements:

<td th:switch="${#lists.size(teacher.courses)}">
    <span th:case="'0'">NO COURSES YET!</span>
    <span th:case="'1'" th:text="${teacher.courses[0]}"></span>
    <div th:case="*">
        <div th:each="course:${teacher.courses}" th:text="${course}"/>

Depending on the size of the teacher.courses list, we either display a default text, the single course or all courses available. We use the asterisk (*) for the default option.

4. Conclusion

In this short article, we investigated the different types of Thymeleaf conditionals and presented some simplified examples showing the various options.

The examples can be found in the GitHub project.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)