Integration with non-servlet environment

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

Integration with non-servlet environment

zcourts
I'm currently looking to replace our use of Freemarker and Thymleaf is on the list of projects that could serve as a replacement.
At first glance it looks great and is likely the one I go with but a few things are amiss (in my head probably) that I haven't been able to find by scanning the docs. I think this is due to all the examples being done for servlet containers...
1. How does a POJO get associated with a template?
2. Are the th:* attributes removed in the rendered HTML or are they published, including template variable names etc? If they do, is there an option to strip them?
Given it won't be run in a servlet container. We're using Netty, an NIO lib. Until recently Netty was only for static file serving but it was nicer to work with and had other properties preferred over a servlet container such as Tomcat. Freemarker was introduced but as the templates became more complex it became a pain. (Background done).
Can I have a suggestion of what'd make a good starting point of integrating outside of a container?
Reply | Threaded
Open this post in threaded view
|

Re: Integration with non-servlet environment

Zemi
Administrator
Hello,

Thymeleaf can work outside a servlet environment, if you take a look at the "Using Thymeleaf" tutorial at the documentation page, Thymeleaf is configured and used directly in Java.

If you want a quick start, this integration test has a method to initialize Thymeleaf and another one to process a template.

The th:* attributes are removed in the rendered HTML.

> 1. How does a POJO get associated with a template?
I really don't understand this question, could you explain it a bit more?
Reply | Threaded
Open this post in threaded view
|

Re: Integration with non-servlet environment

zcourts
Thanks for the quick reply.
The Test is very useful. It ties it all together for me. I scanned the doc you mentioned before but I missed the IContext stuff. Having a better idea now, that confusing question should have been "How does Thymeleaf get the variables (model) it replaces in the templates?".

But that's also clear now. Its via the context you provide and using ctx.setVariable(name,value);
That being said, I also see you can do ${user.name} => user.getName() on a POJO which also answers that confusing question.

Thanks again, seeing that test was useful.
Reply | Threaded
Open this post in threaded view
|

Re: Integration with non-servlet environment

Zemi
Administrator
I think you'll find the missing pieces in the setUp() method of another test which extends the former.

What you need to set variables is:

   Context context = new Context();
   context.setVariable("myVariable", myObject);

Reply | Threaded
Open this post in threaded view
|

Re: Integration with non-servlet environment

zcourts
Got it, thanks.