Retrieving the default SpelEvaluationContext in Thymeleaf / Spring MVC webapp

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

Retrieving the default SpelEvaluationContext in Thymeleaf / Spring MVC webapp

tommyb82

Hi everyone. I am trying to work out how to obtain a reference to the 'default' SpelEvaluationContext instance so that I can, in turn, obtain the TypeLocator to be able to call its registerImport method to add our own package prefixes to it. More info regarding why we want to do this is contained in my original SO question here:

http://stackoverflow.com/questions/24674863/spring-standardtypelocator-in-standardevaluationcontext-registering-new-import

Basically we want to be able to reference our own static types and enums in our templates without always having to fully qualify them. Answer to SO question provided a partial workaround, but I'd still like to know how to get hold of the evaluation context. I can get a reference to the Thymeleaf SpringTemplateEngine via the ThymeleafViewResolver bean.. but from there I'm stuck; I see it gives access to the dialect(s) in use, in this case the SpringStandardDialect..

Any advice greatly appreciated.
Reply | Threaded
Open this post in threaded view
|

Re: Retrieving the default SpelEvaluationContext in Thymeleaf / Spring MVC webapp

danielfernandez
Administrator
This might help you: https://github.com/thymeleaf/thymeleaf-spring/blob/2.1-master/thymeleaf-spring4/src/main/java/org/thymeleaf/spring4/expression/SpelVariableExpressionEvaluator.java#L117-L127

This is what the SpEL Variable Expression Evaluator actually does for obtaining the "base" evaluation context, put there by the ThymeleafView, though note that it is afterwards wrapped in a thymeleaf-enhanced instance...

You can see how ThymeleafView puts it there here: https://github.com/thymeleaf/thymeleaf-spring/blob/e3f32ef14451a1b5181889a5ad972012f61c3222/thymeleaf-spring3/src/main/java/org/thymeleaf/spring3/view/ThymeleafView.java#L249
Reply | Threaded
Open this post in threaded view
|

Re: Retrieving the default SpelEvaluationContext in Thymeleaf / Spring MVC webapp

tommyb82
Hi Daniel - many thanks, I think this is just what I was after. I will let you know how I get on :-)

Thanks,

Tom


On 29 July 2014 10:36, danielfernandez [via Thymeleaf - User Forum] <[hidden email]> wrote:
This might help you: https://github.com/thymeleaf/thymeleaf-spring/blob/2.1-master/thymeleaf-spring4/src/main/java/org/thymeleaf/spring4/expression/SpelVariableExpressionEvaluator.java#L117-L127

This is what the SpEL Variable Expression Evaluator actually does for obtaining the "base" evaluation context, put there by the ThymeleafView, though note that it is afterwards wrapped in a thymeleaf-enhanced instance...

You can see how ThymeleafView puts it there here: https://github.com/thymeleaf/thymeleaf-spring/blob/e3f32ef14451a1b5181889a5ad972012f61c3222/thymeleaf-spring3/src/main/java/org/thymeleaf/spring3/view/ThymeleafView.java#L249


To unsubscribe from Retrieving the default SpelEvaluationContext in Thymeleaf / Spring MVC webapp, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Retrieving the default SpelEvaluationContext in Thymeleaf / Spring MVC webapp

tommyb82
In reply to this post by danielfernandez
Hi Daniel - apologies but I am still unable to get hold of the evaluation context instance being used. I have traced through the code you sent me the links to (and quite a lot of related code!), and I can see it gets added to the variables in a thymeleaf SpringWebContext instance which is available to templates as #ctx, but I can't figure out how to get a handle on that in a controller or (ideally) at application startup.  If a new EvaluationContext instance is created for each view then I guess that wouldn't be possible anyway.

Thanks for your help anyway, and great work on Thymeleaf.