Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

balteo
Hello,

I have the following code in a spring service:

@Override
	public void doMailTest() {
		try {
			final Context ctx = new Context();
			ctx.setVariable("message", "hello");
			final MimeMessage mimeMessage = mailSender.createMimeMessage();
			final MimeMessageHelper message = new MimeMessageHelper(
					mimeMessage, true /* multipart */, "UTF-8");
			message.setSubject("Email test ppptr");// TODO
			message.setFrom("me@company.com");
			message.setTo("balteo@yahoo.fr");
			final String htmlContent = templateEngine.process(
					"test-template.html", ctx);
			message.setText(htmlContent, true /* isHtml */);
			mailSender.send(mimeMessage);
		} catch (MessagingException messagingException) {
			throw new RuntimeException("MessagingException", messagingException);
		}
	}

Here is my template:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <p th:text="${message}"></p>
    </body>
</html>

I get the following exception:
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "message" (test-template.html:7)
	at org.thymeleaf.spring3.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:108)
	at org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:116)
	at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:394)
	at org.thymeleaf.standard.expression.Expression.execute(Expression.java:231)
	at org.thymeleaf.standard.expression.StandardExpressionExecutor.executeExpression(StandardExpressionExecutor.java:70)
	at org.thymeleaf.standard.expression.StandardExpressionExecutor.executeExpression(StandardExpressionExecutor.java:58)
	at org.thymeleaf.standard.expression.StandardExpressionProcessor.executeExpression(StandardExpressionProcessor.java:124)
	at org.thymeleaf.standard.expression.StandardExpressionProcessor.processExpression(StandardExpressionProcessor.java:164)
	at org.thymeleaf.standard.processor.attr.AbstractStandardTextChildModifierAttrProcessor.getText(AbstractStandardTextChildModifierAttrProcessor.java:60)
	at org.thymeleaf.processor.attr.AbstractTextChildModifierAttrProcessor.getModifiedChildren(AbstractTextChildModifierAttrProcessor.java:59)
	at org.thymeleaf.processor.attr.AbstractChildrenModifierAttrProcessor.processAttribute(AbstractChildrenModifierAttrProcessor.java:58)
	at org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:74)
	at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)
	at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:914)
	at org.thymeleaf.dom.Node.processNode(Node.java:869)
	at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:638)
	at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:620)
	at org.thymeleaf.dom.Node.processNode(Node.java:888)
	at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:638)
	at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:620)
	at org.thymeleaf.dom.Node.processNode(Node.java:888)
	at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:638)
	at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:620)
	at org.thymeleaf.dom.Node.processNode(Node.java:888)
	at org.thymeleaf.dom.Document.process(Document.java:93)
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1243)
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1148)
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1095)
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1008)
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:982)
	at com.prosodie.ppptr.services.impl.MailerServiceBean.doMailTest(MailerServiceBean.java:35)
	at com.prosodie.ppptr.services.impl.MailerServiceBeanTest.testSendEmail(MailerServiceBeanTest.java:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NullPointerException
	at org.thymeleaf.spring3.expression.Themes.<init>(Themes.java:53)
	at org.thymeleaf.spring3.expression.SpelVariableExpressionEvaluator.computeExpressionObjects(SpelVariableExpressionEvaluator.java:165)
	at org.thymeleaf.spring3.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:88)
	... 60 more

I am not sure why as my template looks OK.

Can anyone please help?

Regards,

J.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

flim
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

balteo
Hello Flim,
Thanks for replying. Ummm. What do you mean. My message only contains "hello". Doesn't it?
Anyway, wouldn't it raise a parseexception instead if there were characters such as gt or lt?
Julien.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

flim
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

balteo
Good point about the NPE! I had missed that one.

Can anyone please advise how to use Thymeleaf in a non-web environment? I basically just want to be able to send email from a Spring service layer and I don't understand:

-Why I need springmvc dependencies
-Why I get this web-layer-related NPE exception about Themes...

Can anyone please help?

Regards,

Julien.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

Emanuel
Administrator
This post was updated on .
I wrote the Themes utility object for the last version of thymeleaf-spring3, but never thought about offline templates when doing so (so a request context (which is what the Themes object is throwing NPEs about) was always available).  I'm looking into this now.

I don't know much about the Spring Web MVC dependency, but that should be as simple as changing that to a 'provided' dependency type in the pom.xml.  I'll bring that up Daniel.

[EDIT]: The Spring Web MVC dependency is already set as provided and optional, but the Themes object makes use of the request context, so it might be that which is causing you to include Spring Web MVC in your dependency hierarchy to resolve possible ClassDefNotFoundErrors.  I'll have to figure out a way to decouple these 2 things so that offline processing works without it.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

danielfernandez
Administrator
In reply to this post by balteo
@balteo

In order to use Thymeleaf for sending email from the business layer of a Spring-enabled application, you don't need Spring MVC at all. Simply, don't use a Spring-enabled Template Engine (SpringTemplateEngine class), but merely the normal org.thymeleaf.TemplateEngine, without Spring support. No need to add the "thymeleaf-spring3" package to your classpath therefore.

This will default you to using OGNL as an expression language instead of SpringEL (their syntax is 95% interchangeable), and i18n message resolution will be done using the standard (non-Spring) way in Thymeleaf: by looking to .properties files with the same name as the templates, located in the same folder.

But besides that, you should have no issues processing your email with thymeleaf without Spring MVC, even if your web layer uses GWT, JSF, Struts or whatever.

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

danielfernandez
Administrator
In reply to this post by balteo
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

balteo
Thanks a lot Daniel and Emmanuel!
I now use org.thymeleaf.TemplateEngine and it works.
Thanks again for this great templating engine!
Regards,
Julien.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

frandevel
In reply to this post by danielfernandez
Good point @danielfernandez, this was driving me a bit crazy lately. Also being able to save some space in the war removing the spring-thymeleaf dependency is good.

Thanks a lot for addressing this here,

Fran
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

foxlol
In reply to this post by balteo
Hi.

How did you manage to work?

I have the same scenario and i'm using the 2.0.17 version but i always get the same execption as you.

Can you help me?

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

balteo
Hi,
I actually used "org.thymeleaf.TemplateEngine" instead of the spring counterpart ("org.thymeleaf.spring3.SpringTemplateEngine") as Daniel advised.
Let me know if you got it working.
Regards,
Julien.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed with: TemplateProcessingException: Exception evaluating SpringEL expression

venkatesh
In reply to this post by flim
Hi i also getting the above exception ,can u plaese help me