File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes Journal Article - Velocity - An Introduction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Journal Article - Velocity - An Introduction" Watch "Journal Article - Velocity - An Introduction" New topic
Author

Journal Article - Velocity - An Introduction

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
In the March 2004 edition of the JavaRanch Journal, Thomas Paul has included an article - "Velocity - An Introduction". If you've been wanting to get your feet with a template framework in your web applications, this is a darn good introduction.
Please post comments in this thread.
[ March 09, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

This is amazing!!!
I was just looking for something on Veloocity just to get me started on it. And this article is what I needed!!!
Thanks a lot Thomas Paul!! As usual a very well written and easy to understand artcile. More later....
- Manish
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Thanks!!!


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

Tom, have you ever looked at FreeMarker? It appears to be pretty much the same as velocity with a few extra features.
I would be interested in hearing your opinion on it.


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

Doesn't JSP 2.0's EL try and do pretty much the same thing as Velocity now? I mean if I do something like...
request.setAttribute("user", "gregg");
in my Servlet then forward to a JSP I can just do
${requestScope.user}
And it will spit out Gregg??
And I can use JSTL to iterate over over a collection like

The reason I ask is because in another post you mentioned Velocity is what you thought JSP should have been. So short of still allowing Java Code in a scriplet, doesn't the EL combined with JSTL bring it closer if not to the same thing?
And then of course, after reading this, I still don't think a Frameworks forum is the right place for Velocity discussions.
[ March 09, 2004: Message edited by: Gregg Bolinger ]
Hema Menon
Ranch Hand

Joined: Oct 29, 2000
Posts: 569
Thank you Thomas! Just what I've been looking for. I am trying to understand some of the velocity templates used by Jetspeed portal. Your article gives a quick introduction to velocity.
As always, Thanks!
Hema


~hm
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Gregg Bolinger:
Doesn't JSP 2.0's EL try and do pretty much the same thing as Velocity now? I mean if I do something like...
The problem is that using JSP's will lead even good programmers to take shortcuts. Plus it is harder to teach JSP (even the right way) to someone who designs web sites. When I wrote my own view framework many years ago, one of my key goals was that I should be able to explain it completely to an HTML expert in less than one hour. Velocity passes this test. JSP does not. Plus a Velocity page actually looks sort of like the end resulting page in a broswer. Take my last template example and display it in a browser. It's actually quite easy to see what the resulting page is going to look like without having a Java programmer near by.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Gregg Bolinger:
Tom, have you ever looked at FreeMarker? It appears to be pretty much the same as velocity with a few extra features.
The problem that I have with "features" is tbat all they really add is complexity. I haven't looked at FreeMarker in any detail but one thing is defintely wrong on their comparison page. Development of Velocity has not been abandoned as they claim.
Durgaprasad Guduguntla
Ranch Hand

Joined: Oct 20, 2003
Posts: 99
This article is simple and nice. But when I tried this example on Tomcat, I am getting an exception:
I placed hello.vm in the WEB-INF/templates folder only. Does anyone get this error. Please advise.


Thanks,<br />Durgaprasad<br />SCJP1.4, SCWCD1.4, SCBCD1.3,<br />SCEA
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

Originally posted by Thomas Paul:
The problem is that using JSP's will lead even good programmers to take shortcuts. Plus it is harder to teach JSP (even the right way) to someone who designs web sites. When I wrote my own view framework many years ago, one of my key goals was that I should be able to explain it completely to an HTML expert in less than one hour. Velocity passes this test. JSP does not. Plus a Velocity page actually looks sort of like the end resulting page in a broswer. Take my last template example and display it in a browser. It's actually quite easy to see what the resulting page is going to look like without having a Java programmer near by.

Ahh, ok Tom, I see. Thanks for explaining that.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Durgaprasad Guduguntla:
This article is simple and nice. But when I tried this example on Tomcat, I am getting an exception:
I placed hello.vm in the WEB-INF/templates folder only. Does anyone get this error. Please advise.

What do your web.xml and velocity.properties files look like?
Durgaprasad Guduguntla
Ranch Hand

Joined: Oct 20, 2003
Posts: 99
Thanks for your reply. I have corrected the parameter file.resource.loader.path in velocity.properties file to the correct path. It worked fine.
Greg Nudelman
Greenhorn

Joined: Feb 25, 2002
Posts: 2
Good article! We've been using Velocity for the last year in production and found it a "superb" development environment. As to Velocity vs. JSP question, V is a much better solution, because it has:
1) Natural, easy to read, learn, and remember syntax (no curly braces, no XSLT, or goofy loops...)
2) Template is easily visible as HTML in the browser
3) No import statements, or any other junk in the template
4) quick reload (no compilation step as in JSP)
5) Error reporting is superb (JSP often points to line 1000 of your 500-line template, forcing you to go find and debug the compiled template)
6) No temptation of putting Java code in the template.
7) Excellent foreach construct and null object handling
Velocity does just one thing, but it does it "superbly". I truly believe using it shaved several months off our total effort, and made development a pleasure again. Anyone interested in solid MVC development practices should check it out.
Thanks again for getting the word out!
Greg Nudelman


I'm alergic to Mondays
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

Originally posted by Greg Nudelman:
Good article! We've been using Velocity for the last year in production and found it a "superb" development environment. As to Velocity vs. JSP question, V is a much better solution, because it has:
1) Natural, easy to read, learn, and remember syntax (no curly braces, no XSLT, or goofy loops...)
2) Template is easily visible as HTML in the browser
3) No import statements, or any other junk in the template
4) quick reload (no compilation step as in JSP)
5) Error reporting is superb (JSP often points to line 1000 of your 500-line template, forcing you to go find and debug the compiled template)
6) No temptation of putting Java code in the template.
7) Excellent foreach construct and null object handling
Velocity does just one thing, but it does it "superbly". I truly believe using it shaved several months off our total effort, and made development a pleasure again. Anyone interested in solid MVC development practices should check it out.
Thanks again for getting the word out!
Greg Nudelman

Good points Greg. Thanks for sharing this info. All that's left for my project is for me to commit to velocity. Wish me luck!
Andy Cooke
Greenhorn

Joined: May 14, 2004
Posts: 3
Hi Thomas,

thanks for the article - I had heard of the advantages of velocity before, and your article has helped me understand some of the basics.

I'd like to use Velocity to support a simple web-app that I am building. This has some static pages, and some dynamically generated pages (xslt, etc.).

I'd like to minimise html code duplication... thus I would like the home page, and every other page to import the site's banner from one separate file.

How can I achieve this using Velocity? Specifically:

* How can I configure tomcat so that if a user requests a default/home page such as "http://www.url.com/myapp" or "http://www.url.com/myapp.index.html", they get a velocity-generated page?

* Do I need to write a servlet to support every template that I use? e.g. Can I simply write a (static) index.vm file, or do I also need to provide an Index servlet (that wouldn't do much)?

* If I have to resort to JSP or server-side includes to achieve this, then is it possible for a velocity template to "include" a file defined in another format (e.g. banner.jsp)?

thanks for any tips,

Andy Cooke
Andy Cooke
Greenhorn

Joined: May 14, 2004
Posts: 3
Hi again,

I'll reply to my last message :-)

Velocity also has some nifty tools. This includes VelocityViewServlet and VelocityLayoutServlet - which they say is "designed with a pull-MVC model in mind".

This means that users can request pages, such as "index.vm", "people.vm" - the request is forward to the VelocityLayoutServlet, which in turn processes the templates.

I define a default layout in default.vm, which sets the header, footer, etc.
Then the content for each page is defined in separate files, and pulled in using "$screen_content".

simple!

Andy Cooke
Yeray Caballero
Greenhorn

Joined: Dec 24, 2004
Posts: 1
This is a very good introduction to Velocity, however I don't get run it. I always get this message:

StandardWrapperValve[hellotest]: Allocate exception for servlet hellotest
javax.servlet.ServletException: Error allocating a servlet instance

Do you know why? thanks for your work.
Archana Babu
Greenhorn

Joined: Nov 22, 2004
Posts: 6
Hi,
I'm new to velocity. I'm trying to get the integration of velocity and struts going.

VelocityViewServlet is being used to handle all the calls to *.vm files.

Find below the snippet from web.xml:


Also a piece of struts-config:



The properties set in velocity.properties are:


While processing homepagetemplate, ResourceNotFoundException is thrown because it has


It tries to find the template directly under the web app working directory and not from the root specified from the properties file ("templates").

Could some one point me out the point at which i'm doing it incorrectly?

I also found this piece of code in VelocityViewServlet:


This always returns the template name from the servletPath(). So when would the VelocityContext properties (for root of template etc.) be used??

Thank you,
Archana.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
I'm going to move this to the Application Frameworks forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Journal Article - Velocity - An Introduction
 
Similar Threads
Journal Article - Automate the build process of your J2ME project with Antenna
utitility to find code metrics? (lines of code, etc)
Journal Article - Automate the build process of your J2ME project with Antenna
Help me in AJAX
Static class members in Velocity