Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot 2.4.2 and Thymeleaf 3.0.12 - access static methods

Since I switched to Spring Boot 2.4.2 my Thymeleaf templates are broken. When I want to access a static member in Spring Controller I get the following error:

Exception processing template "template_name": Instantiation of new objects and access to static classes is forbidden in this context.

The code looks like: th:text="${T(com.test).testMethod("1234")}"

Do you have any recommendation to fix this?

like image 432
christoph Avatar asked Feb 04 '21 14:02

christoph


People also ask

How to create a web application in Spring Boot using thymeleaf?

You will have to follow the below steps to create a web application in Spring Boot by using Thymeleaf. Use the following code to create a @Controller class file to redirect the Request URI to HTML file − In the above example, the request URI is /index, and the control is redirected into the index.html file.

How many types of HTML5 templates are there in Spring Boot?

It contains 6 types of templates as given below − All templates, except Legacy HTML5, are referring to well-formed valid XML files. Legacy HTML5 allows us to render the HTML5 tags in web page including not closed tags. You can use Thymeleaf templates to create a web application in Spring Boot.

How do I access model attributes in thymeleaf?

In Thymeleaf, these model attributes (or context variables in Thymeleaf jargon) can be accessed with the following syntax: $ {attributeName}, where attributeName in our case is messages. This is a Spring EL expression.

How do I access spring beans in thymeleaf?

Spring beans. Thymeleaf allows accessing beans registered at the Spring Application Context with the @beanName syntax, for example: In the above example, @urlService refers to a Spring Bean registered at your context, e.g. This is fairly easy and useful in some scenarios.


Video Answer


3 Answers

Another quick fix is the use of th:with

th:text="${testText}"
th:with="testText=${T(com.test).testMethod("1234")}"

Source/Kudos: https://github.com/thymeleaf/thymeleaf/issues/816#issuecomment-791921248 and https://github.com/thymeleaf/thymeleaf/issues/816#issuecomment-826401631

like image 166
RiZKiT Avatar answered Oct 10 '22 04:10

RiZKiT


There is a workaround to use method from beans registered at the Spring Application Context with the @beanName syntax. Like this:

<div th:text="${@testService.testMethod('123')}">...</div>

http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

like image 20
michal.jakubeczy Avatar answered Oct 10 '22 05:10

michal.jakubeczy


This change is the part of Thymeleaf 3.0.12. They improve restricted expression evaluation mode security by restriction of the access to static code (@identifier@ in OGNL, T(identifier) in SpringEL). What they have done by themselves? ... "Avoided instantiation of new objects and calls to static classes" as stated in release notes. You may move the JAVA calls into your controller and put the result into the view model. After just access this variable from Thymeleaf template.

like image 21
Slava Ivanov Avatar answered Oct 10 '22 05:10

Slava Ivanov