Chinovski
Chinovski

Reputation: 517

How to pass an expression in thymleaf fragment param?

I am trying to display a thymleaf page, but I got multiple errors while I implemented different solution.

I have the following fragment:

<div th:fragment="documentField(field, fieldLabel, isMandatory)">

So I tried the following code to display this fragment:

<div th:replace="fragments/document.html 
    :: documentField(field='Certification', 
        fieldLabel='CERTIFICATION_PROCEDURE', 
        isMandatory='(${#boolsIsTrue(#session.getAttribute('isCertificate'))}')">
</div>

I got this error:

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "~{fragments/document.html 
    :: documentField(field='Certification', 
        fieldLabel='CERTIFICATION_PROCEDURE', 
        isMandatory='(${#boolsIsTrue(#session.getAttribute('isCertificate'))}')}" (template: "application/certification-documents" - line 47, col 8)

I tried the parameter isMandatory without using #boolsIsTrue, and also declaring a variable for #session.getAttribute('isCertificate')) without success.

isCertificate attribute is passed as boolean.

Do you have any idea how to resolve this?

Upvotes: 0

Views: 255

Answers (1)

andrewJames
andrewJames

Reputation: 21916

If you want to pass true or false as the value for your isMandatory parameter, you can use this:

isMandatory=${#session.getAttribute('isCertificate')}

Don't use an enclosing set of apostrophes, or the parentheses '(...)'. Those are not needed here. The #boolsIsTrue() function is also not needed - it serves a different purpose (similar to th:if).

The revised div:

<div th:replace="fragments/document.html 
    :: documentField(field='Certification', 
        fieldLabel='CERTIFICATION_PROCEDURE', 
        isMandatory=${#session.getAttribute('isCertificate')})">
</div>

Upvotes: 1

Related Questions