开发者

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

开发者 https://www.devze.com 2023-02-18 18:58 出处:网络
I\'d like to use Facelets to generate HTML contents. I want to reuse existing taglibs, which isn\'t supported by Velocity.

I'd like to use Facelets to generate HTML contents. I want to reuse existing taglibs, which isn't supported by Velocity.

开发者_如何转开发

I have read the developer's guide, but didn't get a clue.

Does Facelets need a Java compiler to work? (I guess not) I've also tried with Jetty ServletTester, but it seems not work.

So is it possible?


Yes, it's possible.

It's not necessary to have a Java compiler to render the view. Facelets is completely taglib-driven, only EL (expression language) may be occurred in the template script, so nothing needs to be static compiled.

To embed Facelets in a standalone application, you can programmatic initialize a servlet context. I'm using Jetty in this case. See the implementation of ServletTester class, and write you own server.

It's valuable to mention that jsf-ri doesn't work well, however, luckily we have another choice, we can use myfaces-impl which does better.

The main problem remains is about ResourceResolver. If you need to setup a complicated resource structure, i.e., the template files (xhtml) are placed in different locations, then you need to:

  1. Create your own ResourceResolver delegates DefaultResourceResolver.

  2. Override org.mortbay.jetty.servlet.Context.getResource() to make it returns consistent resources with the ResourceResolver. Or similar ones if the embedded servlet context isn't Jetty-based.

0

精彩评论

暂无评论...
验证码 换一张
取 消