let me down slowly
let me down slowly

Reputation: 1017

How to use variables from a Constants class in thymeleaf numbers.sequence() method?

I have two static variables defined in a Constants class like this

public static String MIN_VALUE = 1;
public static String MAX_VALUE = 10;

I want to create a html select tag with options with all numbers starting from MIN_VALUE to MAX_VALUE. I can easily do this if I use hardcoded numbers in thymeleaf:

<select>
  <option th:each="number: ${#numbers.sequence(1, 10)}" th:value="${number}" th:text="${number}"/>
</select>

But if I try something like this:

<option th:each="number: ${#numbers.sequence(${Constants.MIN_VALUES}, ${Constants.MAX_VALUES})}" th:value="${number}" th:text="${number}"/>

I get this error:

EL1043E: Unexpected token. Expected 'rparen())' but was 'lcurly({)'

How can I use variable from my Constant class in thymeleaf's numbers.sequence method?

I found this question with almost similar title, but I am not passing my variable limits via model attribute here. I can access my variables from Constants class using Constants.VAR_NAME from thymeleaf tags.

Upvotes: 2

Views: 3536

Answers (1)

Metroids
Metroids

Reputation: 20487

  1. Don't nest ${...} expressions.
  2. Use T(com.example.Constants).MIN_VALUE as stated in the comments. (Where com.example.Constants is the complete package and class name.)

Example:

<select>
  <option
    th:each="number: ${#numbers.sequence(T(com.example.Constants).MIN_VALUE, T(com.example.Constants).MAX_VALUE)}"
    th:value="${number}"
    th:text="${number}" />
</select>

Upvotes: 2

Related Questions