0

How to access from Thymeleaf to constant?

public final static int SOME_CONSTANT = 255;

I am using the following code:

@com.company.project.classWithConstants@SOME_CONSTANT

And I am getting the following exception:

com/company/project/classWithConstants (wrong name: com/company/project/ClassWithConstants)

(There is a similar question at SO, but OP is using spring: Access from Thymeleaf to class field)

kmasalski
  • 238
  • 4
  • 17
  • 1
    Might be a typo? Try com.company.project.ClassWithConstants. If it doesn't work, can you try renaming your constant to someConstant and adding a getter with the name getSomeConstant and see if what you are using works? Might help getting an answer, and also for science :) – Jorge.V Aug 28 '19 at 11:44
  • @Jorge.V ClassWithConstants works. Thanks. – kmasalski Aug 28 '19 at 13:05

1 Answers1

0

As you know, Thymeleaf uses OGNL to process commands. So, Without Spring and SpEL this can be achieved like this:

<td>
    <input class="form-control" type="text" name="inputValue"
        th:maxlength="${@com.example.yourpackagename.Constants@MAX_TESTCASE_VALUE_LENGTH}" />
</td>

OGNL documentation

Ghasem Sadeghi
  • 1,734
  • 13
  • 24