• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Journal Article - Velocity - An Introduction

 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!!
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. I have corrected the parameter file.resource.loader.path in velocity.properties file to the correct path. It worked fine.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to move this to the Application Frameworks forum.
reply
    Bookmark Topic Watch Topic
  • New Topic