Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thymeleaf-spring5 ClassNotFoundException ognl/PropertyProcessor #203

Open
tcuje opened this issue Jan 8, 2019 · 8 comments
Open

thymeleaf-spring5 ClassNotFoundException ognl/PropertyProcessor #203

tcuje opened this issue Jan 8, 2019 · 8 comments

Comments

@tcuje
Copy link

tcuje commented Jan 8, 2019

I tried to use thymeleaf in my project, but get the following exception:
java.lang.NoClassDefFoundError: ognl/PropertyAccessor

My versions copied from mvn dependency:tree

org.thymeleaf:thymeleaf-spring5:jar:3.0.11.RELEASE:compile
  \- org.thymeleaf:thymeleaf:jar:3.0.11.RELEASE:compile
     +- org.attoparser:attoparser:jar:2.0.5.RELEASE:compile
     \- org.unbescape:unbescape:jar:1.1.6.RELEASE:compile

I think the issue here is that ognl is excluded by thymeleaf-spring5/pom.xml, see https://github.com/thymeleaf/thymeleaf-spring/blob/3.0-master/thymeleaf-spring5/pom.xml#L318

Unfortunately the relevant commits do not state why it was excluded, see b6a4911 and 896a147

@ultraq
Copy link
Member

ultraq commented Jan 9, 2019

OGNL is excluded when Thymeleaf is in a Spring project (when including the thymeleaf-spring module) so that the Spring Expression Language (SpEL) is used over OGNL in your templates.

If you've got a Spring Boot project, then this should be taken care of automatically if you import the right starter modules and so no use of OGNL will come up and no ClassNotFoundException should be thrown. If you're using Spring Framework and manually configuring your app to use Thymeleaf, then the setup/configuration is a bit more involved but is described in the Thymeleaf + Spring Tutorial doc which you can find here: https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#the-springstandard-dialect

@tcuje
Copy link
Author

tcuje commented Apr 2, 2019

Im using the following package for the project:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

It's the only reference to thymeleaf I have in my pom.xml

@mermetbt
Copy link

I had the same issue as you, and adding this solve the problem for me:

        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>3.1.12</version>
        </dependency>

@lurodrig
Copy link

Hello there,

In my case I am using it in a junit test:

import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.thymeleaf.TemplateEngine;

/**
 *
 * @author lurodrig
 */
@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties
@SpringBootTest(classes = {MessageFactoryServiceImpl.class,TemplateEngine.class})
public class MessageFactoryServiceTest {
    
    @Value("${test.emailAddress}")
    private String emailAddress;
    
    @Value("${test.barcodeText}")
    private String barcodeText;
    
    @Value("${test.registrationLink}")
    private String registrationLink;
    
    @Value("${test.barcodeBase64}")
    private String barcodeBase64;
    
    @Autowired
    private MessageFactoryService messageFactoryService;
    
    @Test
    public void test_A_emailTextIsRightlyProcessed() {
        Map<String, Object> messageVariables = new HashMap<>();
        messageVariables.put(TemplateConstants.EMAIL_ADDRESS, emailAddress);
        messageVariables.put(TemplateConstants.BARCODE_BASE_64, barcodeBase64);
        messageVariables.put(TemplateConstants.BARCODE_TEXT, barcodeText);
        messageVariables.put(TemplateConstants.REGISTRATION_LINK, registrationLink);
        String message = messageFactoryService.buildVerificationMessage(messageVariables);
        System.out.println(message);
    }
}

The trick of @mermetbt worked for me...

        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>3.2.10</version>
            <scope>test</scope>
        </dependency>

But now I am facing the issue (yet another :) of spring not taking into account the configuration from src/test/resources/application.properties for thymeleaf :(

Thanks,

Luis

@amanbhole
Copy link

amanbhole commented Nov 12, 2020

After lot of experimentation, following line makes the issue OGNL issue disappear for both Spring and Spring boot:
TemplateEngine templateEngine = new SpringTemplateEngine();

Further, for spring boot to work properly, I had to configure engine as below:
@Bean @Primary public TemplateEngine textTemplateEngine() { TemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); return templateEngine; }

As my use case is about simple string replacement in text templates, following is template resolver configuration:
private ITemplateResolver templateResolver() { StringTemplateResolver templateResolver = new StringTemplateResolver(); templateResolver.setTemplateMode(TemplateMode.TEXT); return templateResolver; }

@tcuje
Copy link
Author

tcuje commented Jun 9, 2022

I can confirm that replacing TemplateEngine templateEngine = new TemplateEngine(); with
TemplateEngine templateEngine = new SpringTemplateEngine();
as suggested by @amanbhole worked for us, too. Much thanks!

umeshwaghode pushed a commit to checkmarx-ltd/cx-flow that referenced this issue Jun 24, 2022
* Downgrading OGNL due to thymeleaf/thymeleaf-spring#203.

* Removing extra commented lines.
satyamchaurasiapersistent added a commit to checkmarx-ltd/cx-flow that referenced this issue Jul 4, 2022
* Downgrading OGNL due to thymeleaf/thymeleaf-spring#203 (#1041)

* Downgrading OGNL due to thymeleaf/thymeleaf-spring#203.

* Removing extra commented lines.

* Allow zip exclude via config as code (#1044)

* Allow the zip-exclude property to be set via config-as-code

* Update config-as-code documentation

* Fixed closing and reopening of issues for all bug trackers (#1045)

* Fixed closing and reopening of issues for all bug trackers

* Updated test case data

Co-authored-by: Leonel Sanches <[email protected]>
Co-authored-by: James Bostock <[email protected]>
Co-authored-by: HussainS12 <[email protected]>
satyamchaurasiapersistent added a commit to checkmarx-ltd/cx-flow that referenced this issue Jul 4, 2022
* CXFLW-400 Adding changes for break build.

* CXFLW-383 Removed name from comments

* Test case FIX for CMD line argument.

* Merging Development changes in my branch (#1047)

* Downgrading OGNL due to thymeleaf/thymeleaf-spring#203 (#1041)

* Downgrading OGNL due to thymeleaf/thymeleaf-spring#203.

* Removing extra commented lines.

* Allow zip exclude via config as code (#1044)

* Allow the zip-exclude property to be set via config-as-code

* Update config-as-code documentation

* Fixed closing and reopening of issues for all bug trackers (#1045)

* Fixed closing and reopening of issues for all bug trackers

* Updated test case data

Co-authored-by: Leonel Sanches <[email protected]>
Co-authored-by: James Bostock <[email protected]>
Co-authored-by: HussainS12 <[email protected]>

Co-authored-by: Satyam Chaurasia <[email protected]>
Co-authored-by: Leonel Sanches <[email protected]>
Co-authored-by: James Bostock <[email protected]>
Co-authored-by: HussainS12 <[email protected]>
umeshwaghode pushed a commit to checkmarx-ltd/cx-flow that referenced this issue Jul 13, 2022
@AlperMulayim
Copy link

AlperMulayim commented Apr 14, 2023

Hello Everyone,
@tcuje's solution worked for me.
TemplateEngine templateEngine = new SpringTemplateEngine();
SpringBoot version: 2.7.11-SNAPSHOT
Thanks.

@Nonobeam
Copy link

Nonobeam commented Nov 28, 2024

Some how in 2024 I am still facing this issue. For anyone that need deep inside, this issue happen because spring automatically use Default Template engine instead of SpringTemplateEngine, this will lead to the need of ognl. @tcuje solution tell Spring that it have to use SpringTemplateEngine instead of TemplateEngine. Btw, for anyone wonder what is templateResolver(). Below code may help you a little bit.

private final TemplateEngine templateEngine;

public ThymeleafTemplateRenderer() {
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setPrefix("templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setTemplateMode("HTML");

    templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants