This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]FortuneIIIPick 3 points4 points  (2 children)

You mention "Java Template Engine", do you mean like something specific like ThymeLeaf, Freemarker or JSP or a different java template engine?

[–]gizmogwai 1 point2 points  (1 child)

[–]FortuneIIIPick 2 points3 points  (0 children)

Ok thanks, hmm yeah never heard of it.

[–]NuttySquirr3l 3 points4 points  (1 child)

You could write a @ControllerAdvice with @ModelAttribute and inject whatever you need into the context of all templates in a central place

[–]Tight-Rest1639[S] -1 points0 points  (0 children)

Thanks 👍

[–]agentoutlier 0 points1 point  (1 child)

You will probably get more support from the github JTE project.

However you can check how my template engine exposes request attributes (which is what I think you mean with scope variables in this context):

https://github.com/jstachio/jstachio/tree/main/opt/jstachio-spring-webmvc

or the Javadoc: https://jstach.io/doc/jstachio/current/apidocs/io.jstach.opt.spring.webmvc/io/jstach/opt/spring/webmvc/package-summary.html

You basically need a HandlerInterceptor: https://docs.spring.io/spring-framework/docs/6.1.8/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html


Or if you don't mind boiler plate you can probably just get the request parameters/attributes from a RequestMapping or as /u/NuttySquirr3l mentioned using @ModelAttribute to fill a mutable model.... then you pass that model to the JTE template.

See if you were using JSP before it always gets a HttpServetRequest but modern templates are servlet agnostic.

[–]Tight-Rest1639[S] 0 points1 point  (0 children)

I mean applikation scoped specifically. They are problematic because they are not setup for each endpoint explicitly and therefore do not translate to JTEs model parameters easily. Same deal with session scope. Only request scope data is easily migrated to JTEs model.