Thymeleaf3 has no SpringWebContext so how to add beans in the context?

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

Thymeleaf3 has no SpringWebContext so how to add beans in the context?

paranoiabla
Hello,

I was able to programmatically configure Thymeleaf2 to use SpringWebContext which was adding Beans variable in the context:

http://grepcode.com/file/repo1.maven.org/maven2/org.thymeleaf/thymeleaf-spring3/2.0.13/org/thymeleaf/spring3/context/SpringWebContext.java#117

so I could use the following:

<h2 th:text="${beans.environment.getProperty('spring.mail.host')}"></h2>

Now with Thymeleaf3 the SpringWebContext class is gone so I am creating a WebContext instead but now I get this exception when I try to render the same snippet:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 6): Property or field 'environment' cannot be found on null
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:220)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81)
	at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:57)
	at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
	at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
	at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:267)
	at org.thymeleaf.spring4.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263)

I also tried using the @-notation:
<h2 th:text="${@environment.getProperty('spring.mail.host')}"></h2>
 but then I got the following exception:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): No bean resolver registered in the context to resolve access to bean 'environment'
	at org.springframework.expression.spel.ast.BeanReference.getValueInternal(BeanReference.java:51)
	at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:51)
	at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
	at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
	at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:267)
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf3 has no SpringWebContext so how to add beans in the context?

paranoiabla
Anyone here? Basically what I am asking is: how can I execute programmatically thymeleaf so that it can find any spring bean?
Reply | Threaded
Open this post in threaded view
|

Re: Thymeleaf3 has no SpringWebContext so how to add beans in the context?

danielfernandez
Administrator
This works for me (using "@environment", the "beans." syntax was already deprecated in 2.1 and removed in 3.0).

Could you please make sure you are actually adding your Environment object as a bean to your application context?