How to use Java Facelets as a generic template engine in a standalone application?

I want to use Facelets to create HTML content. I want to reuse existing taglib that are not supported by Velocity.

I read the developer's guide, but did not understand.

Is Facelets required for a Java compiler? (I think not) I also tried with Jetty ServletTester, but it doesn't seem to work.

So maybe?

+4
source share
1 answer

Yes it is possible.

You do not need to have a Java compiler to render the view. Facelets is fully controlled by taglib, there can only be EL (expression language) in a script template, so nothing needs to be statically compiled.

To insert Facelets into a standalone application, you can programmatically initialize the servlet context. In this case, I am using Jetty. See the implementation of the ServletTester class and write your own server.

It is useful to note that jsf-ri does not work very well, however, fortunately, we have a different choice, we can use myfaces-impl , which works better.

The main problem is ResourceResolver . If you need to configure a complex resource structure, i.e. Template files (xhtml) are located in different places, you need to:

  • Create your ResourceResolver delegates to DefaultResourceResolver .

  • Override org.mortbay.jetty.servlet.Context.getResource() so that it returns matched resources using ResourceResolver . Or similar, if the built-in servlet context is not based on Jetty.

+1
source

Source: https://habr.com/ru/post/1344270/


All Articles