Velocity vs Apache Tiles
What's better? We are going to start a new web project and it's a question which technology to cho开发者_运维技巧ose. Project includes Spring MVC + WebFlow. Any good/bad experience in support, extending, performance?
Thank you.
Velocity and Tiles are two different beasts — Tiles is a layout engine and Velocity is a template engine. They do not intersect anywhere in terms of functionality.
From your question I can guess you probably won't be using JSP. That's a smart move. Velocity is one of the template engines out there and it does an absolutely splendid job.
And if you choose to follow the template engine route for your view, then check out commentit. It's a small, simple and fast layout engine I created. It might serve your purpose perfectly.
How do you want compare Tiles with Velocity? - Tiles is a way to compose page fragments, while Veleocity is a more complete template engine, better comparable with JSP than Tiles.
Anyway: I used Spring MVC with Tiles and JSP: It worked greatly, saved a lot of time (toward just using JSP, or JSP with Sitemash), and I did not noticed any performance problems. (But the web application was never used under high load.)
Use Tiles if you have some seperate HTML
files that you want to bring together in a Template (i.e. you have a seperate page for a Header, Footer, Sidebar and you want to bring them together and display them in a sort of Newspaper-like format).
Use Velocity if you want to bring dynamic content across from a Java
backend and inject those values into a full HTML
page (i.e. you have a HTML
table to display a selection of Cars, and all of your cars are stored in a Database. Using JPA
, you could get that Car data out of the database, and into a List<Car>
maintained in an EJB
Bean, Spring @Component
, or similar. Then, using Velocity, you can store that information as the bucket item inside a Map
, and use the VTL
mark-up to refer to the key value items in the Map
so that they can be rendered as part of the HTML
response.
To achieve the above, Velocity positions itself as an outright Front Controller Servlet, or is wrapped by another MVC framework (i.e. Spring MVC provides a View Resolver which wraps the functionality provided by a Velocity Servlet).
HTML
requests are directed to the Velocity Servlet or alternative MVC Framework Servlet via web.xml
configuration. As part of the servlet response, your pre-baked HTML
view, complete with Velocity VTL
mark-up, is enriched with Map
data.
Effectively, and in summary:
Velocity competes in the same space as JSP.
Tiles is more akin to page transclusion. In the PHP space, Smarty is a popular cousin.
You don't choose between them, but very well could use both.
There is a Velocity plugin for Tiles 2.2 so you can use both - with velocity you will access context and build your bricks dynamically like with JSP and tiles will combine your website bricks together. However Tiles is not allowing to do many thigns (at least I haven't discovered them yet) and its documentation is very old and bad compared to for example Spring or JSF one. So you can consider using different technology instead.
I have Tiles references in my currect project because Roo did it for me but right now I'm moving everything to JSF.
Use both. Tiles and Velocity integrate very well and solve different problems. You can do some Tiles-ish stuff with Velocity's #include and #parse directives, but Tiles does that composition stuff better.
精彩评论