Conditional attribute using thymeleaf

jsp, thymeleaf

Solution

It's really kind of counter-intuitive, considering to append a conditional class you use the following format:

<div th:classappend="${userSlug != null}?has-slug">

In order to append a conditional attribute, you put the condition on the attribute's value, like so:

<div th:attrappend="data-path=${userSlug != null}?@{/myaccount/__${userSlug}__}">

If userSlug is null, the previous statement will evaluate to:

<div>

If userSlug is not null, the statement evaluates to:

<div data-path="/myaccount/my-slug">

Problem

I know how to make a conditional attribute inside a tag on jstl: ``` <body <c:if test="${userCreated}"> onload="somejavascriptfunction()"</c:if> > ``` But how do I do it using thymeleaf? IndexController ``` @RequestMapping("/register") public String register(UserEntity user, @RequestParam String repeatedPassword, RedirectAttributes redirectAttributes) { user.setAdmin(false); userFacade.create(user); redirectAttributes.addFlashAttribute(Constants.USER_CREATED, true); logger.info("Usuario criado"); return "redirect:/login"; } ``` So far the only solution I found was to do it like this ``` <script type="text/javascript" th:if="${userCreated}"> $(document).ready(function() { somejavascriptfunction() }); </script> ``` But that doesn't seem to be the best way to do it. So how do I make an if statement for an attribute on thymeleaf?

Original source