问题
I've been using ZPT in python recently and I love the templating language. I was looking for something similar for Java but couldn't really find anything I liked as well. The closest thing is FreeMarker.
The problem with FreeMarker and the other Java template engines I looked at was their JSP style syntax that allows for non-conforming XML. I was just wondering if there was a Java template engine that is similar to Zope Page Templates such that it's an "attribute" language that requires valid xml.
回答1:
I think think there quite some of the template engines your looking for:
- Cambridge
- Thymeleaf
- JTP (dead - but exact implementation)
- javaTAL (dead - but exact implementation)
Other approaches supporting valid html are:
- Snippetory (Not bound to html)
- Lift (scala)
回答2:
FreeMarker has a nasty dependency on AWT. It makes it impossible to use with things like Google App Engine.
I prefer to use StringTemplate for all my Java templating needs. It is the only Java based template system that strictly separates the logic from the template.
StringTemplate is a java template engine (with ports for C#, Python, Ruby, and Scala) for generating source code, web pages, emails, or any other formatted text output. StringTemplate is particularly good at multi-targeted code generators, multiple site skins, and internationalization/localization
Its distinguishing characteristic is that it strictly enforces model-view separation unlike other engines. Strict separation makes websites and code generators more flexible and maintainable; it also provides an excellent defense against malicious template authors.
Since you are generating XML
Another solution that isn't obvious at first sight is using JAXB. We have a project here that requires us to generate XML, we have very well defined XSD files for the output files, building the objects and marshalling them is super easy and very painless.
回答3:
The java template engine you will find to be most similar to Chameleon is Thymeleaf.
回答4:
There is Distal for client based templating.
回答5:
There are currently two tal implementations for Java that I know of:
- Java ZPT
- JPT
回答6:
There is also Apache Velocity. Although it doesn't require that your templates be valid XML. That may be a deal breaker for you given the question. You could likely enforce that rule with extensions to the core classes though.
回答7:
What about GXP?
There's also LSP and xtempore.
来源:https://stackoverflow.com/questions/8001398/java-template-library-similar-to-zpt-attribute-language