velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bill Burton <>
Subject Re: template isn't found within a jsp
Date Thu, 05 Sep 2002 23:21:38 GMT
Hello, wrote:
> Hello,
> I want to use velocity for the first time. In the users guide, it was explained
> how to use velocity within a servlet. So I thought, I could use velocity
> directly within a jsp but with the advantage, that I don't have to compile
> the servlet each time I change the code.

What's the problem with recompiling?  Use Ant with a simple build.xml and
it's a breeze.  If you aren't familiar with Ant, I can send you a generic
build.xml for building .java files in a web app.

> So I tried the following code:
> ----
> <%
> Velocity.init();
> VelocityContext context = new VelocityContext();
> context.put( "name", "Hello World");
> // i've also tried: application.getRealPath("/")+"vc_test.vm";
> String templateName = "vc_test.vm";
> Template template = Velocity.getTemplate(templateName);
> template.merge( context, out);
> %>


> the example above works perfectly. But I wonder why the first code sample
> doesn't work.
> If it velocity can be used within a servlet, it must be possible to use
> it within a jsp (without using the <vel:velocity> tag)

So it looks like you want to put your business logic in JSP's and then use
Velocity for the view.

You should look at using some kind of framework to help organize and
structure your application.  See what's available at in the Frameworks
section.  One or more of the frameworks there will let you write logic in
a scripting language such as Rhino (JavaScript), BeanShell (Java like
syntax) or others.  The WebWork framework
( with which I'm most familiar, also
supports writing logic only JSP's.

If you'd rather write your own environment, check out the Velocity View
Tool at  Your JSP
could set objects into the request with request.setAttribute and forward
to the .vm template which would pull them out.  It would still be a good
idea to have a servlet controller or dispatcher that would look at the URL
and dispatch to the appropriate JSP or script then render the desired
Velocity template.

Hope this helps,

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message