You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Nick Temple <nt...@alivecity.com> on 2002/06/12 00:56:57 UTC

Converting JSP's to Velocity

Hi --

I've been playing with Velocity for a few weeks now, and am now in the
process of converting an older MVC app (custom code) to use Velocity as it's
template engine, instead of JSP's (with lots of embedded logic).

The current system uses <jsp:include> to get data for various parts of the
page through the controller servlet. I need to find a way to duplicate that
behavior from within a bean attached to the Velocity context.  In
particular, I'm looking to do something similar to:

  VelocityContext context = new VelocityContext();

  String body;
  String resourcePath =
"/servlet/NavForward?sid=82&req=mInfo_jw1&include=yes&mi=51:2848671&smi=51:2
848671";
  RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
  body = rd.include(request, response);

  context.put("body", body);

Obviously, RequestDispatcher.include modifies the response (directly), and
doesn't return a String -- this is just what I'd "like" to do, conceptually.
I'm not not sure how to make this work, if it is at all possible.

Can anyone suggest an alternative implementation?  Might it be possible to
use an EventHandler to call the include method at the appropriate place in
the stream?  At first glance, buffering issues make it seem like that won't
work, but I haven't tried.

Has anyone else run into this? Is there a solution that does not require me
to immediately rewrite the entire application?

Thanks in advance,

Nick


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Converting JSP's to Velocity

Posted by Tim Pizey <ti...@paneris.org>.
Hi Nick, 

On Wednesday 12 June 2002 17:31, Nick Temple wrote:
>
> I am in the market for an (open source, MVC, servlet) "application
> framework" that I can commit to half-way (writing new code in the new
> framework, re-writing as I have time, while still being able to use the
> legacy application).  It may be that WebWork fits the bill (probably better
> than Turbine, from what I've seen so far).

Have a look at http://www.melati.org/, it is GPL, 
uses Velocity (or WebMacro) and can use 
Postgresql, MySQL, MSQLServer, Mckoidb and HSQLDB as the back end.

Just download the CVS tree and type ant all to have it working locally 
as it comes bundled with hsqldb and jetty, making a pure-java 
development environment.

cheers
Tim P




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Converting JSP's to Velocity

Posted by Nick Temple <nt...@alivecity.com>.
Bill --

Thanks for the pointer.

The code was actually pretty "complicated", requiring a pretty sound
understanding of the internals of servlet processing (InvocationHandler,
etc) -- a better understanding than I currently have, anyway.  However, the
code within WebWork worked well and I learned a lot!

I am in the market for an (open source, MVC, servlet) "application
framework" that I can commit to half-way (writing new code in the new
framework, re-writing as I have time, while still being able to use the
legacy application).  It may be that WebWork fits the bill (probably better
than Turbine, from what I've seen so far).

Nick


-----Original Message-----

Hi Nick,

To include the output of a servlet within a template should not be very
difficult.  All you need to do is write a simple class (a tool) with a
method to call RequestDispatcher passing the parameter of some method.
Then put that class in the VelocityContext.

The WebWork framework (http://sf.net/projects/webwork/) supports this
functionality out of the box.  There's an #includeservlet macro that looks
like it will do exactly what you want.  Even if you don't use WebWork for
your application, you may find some of the code helpful for your
implementation.

-Bill

Nick Temple wrote:
>
> Hi --
>
> I've been playing with Velocity for a few weeks now, and am now in the
> process of converting an older MVC app (custom code) to use Velocity as
it's
> template engine, instead of JSP's (with lots of embedded logic).
>
> The current system uses <jsp:include> to get data for various parts of the
> page through the controller servlet. I need to find a way to duplicate
that
> behavior from within a bean attached to the Velocity context.  In
> particular, I'm looking to do something similar to:
>
>   VelocityContext context = new VelocityContext();
>
>   String body;
>   String resourcePath =
>
"/servlet/NavForward?sid=82&req=mInfo_jw1&include=yes&mi=51:2848671&smi=51:2
> 848671";
>   RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
>   body = rd.include(request, response);
>
>   context.put("body", body);
>
> Obviously, RequestDispatcher.include modifies the response (directly), and
> doesn't return a String -- this is just what I'd "like" to do,
conceptually.
> I'm not not sure how to make this work, if it is at all possible.
>
> Can anyone suggest an alternative implementation?  Might it be possible to
> use an EventHandler to call the include method at the appropriate place in
> the stream?  At first glance, buffering issues make it seem like that
won't
> work, but I haven't tried.
>
> Has anyone else run into this? Is there a solution that does not require
me
> to immediately rewrite the entire application?
>
> Thanks in advance,
>
> Nick

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Converting JSP's to Velocity

Posted by Bill Burton <bi...@progress.com>.
Hi Nick,

To include the output of a servlet within a template should not be very
difficult.  All you need to do is write a simple class (a tool) with a
method to call RequestDispatcher passing the parameter of some method. 
Then put that class in the VelocityContext.

The WebWork framework (http://sf.net/projects/webwork/) supports this
functionality out of the box.  There's an #includeservlet macro that looks
like it will do exactly what you want.  Even if you don't use WebWork for
your application, you may find some of the code helpful for your
implementation.

-Bill

Nick Temple wrote:
> 
> Hi --
> 
> I've been playing with Velocity for a few weeks now, and am now in the
> process of converting an older MVC app (custom code) to use Velocity as it's
> template engine, instead of JSP's (with lots of embedded logic).
> 
> The current system uses <jsp:include> to get data for various parts of the
> page through the controller servlet. I need to find a way to duplicate that
> behavior from within a bean attached to the Velocity context.  In
> particular, I'm looking to do something similar to:
> 
>   VelocityContext context = new VelocityContext();
> 
>   String body;
>   String resourcePath =
> "/servlet/NavForward?sid=82&req=mInfo_jw1&include=yes&mi=51:2848671&smi=51:2
> 848671";
>   RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
>   body = rd.include(request, response);
> 
>   context.put("body", body);
> 
> Obviously, RequestDispatcher.include modifies the response (directly), and
> doesn't return a String -- this is just what I'd "like" to do, conceptually.
> I'm not not sure how to make this work, if it is at all possible.
> 
> Can anyone suggest an alternative implementation?  Might it be possible to
> use an EventHandler to call the include method at the appropriate place in
> the stream?  At first glance, buffering issues make it seem like that won't
> work, but I haven't tried.
> 
> Has anyone else run into this? Is there a solution that does not require me
> to immediately rewrite the entire application?
> 
> Thanks in advance,
> 
> Nick

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ParseErrorException not thrown...

Posted by "Geir Magnusson Jr." <ge...@adeptra.com>.
Can you 

1) try with 1.3-rc1

2) if still present, put in a bug report with bugzilla?



On 6/11/02 10:29 PM, "Normand Gagnon" <ng...@newtradetech.com> wrote:

> Hi,
> 
> My Servlet extends the VelocityServet (release 1.2) and I override the method:
> 
> error(HttpServletRequest request, HttpServletResponse response, Exception ex)
> 
> to create different view depending on the occurring exception for web
> designers or programmers.
> 
> I noticed that ParseErrorException are not thrown to the VelocityServlet
> when occurring in a "three-level" or more level template, that is, a
> template that was parsed by another template that was also parsed by
> another template.  The error log can be found in the Velocity.log but the
> VelocityServlet's error method is never call ending up with a blank or
> partially parsed template as response.
> 
> Exemple:
> 
> template:a.vm
> ------------------------- >
> #parse("b.vm")
> 
> 
> template:b.vm
> --------------------------->
> #parse("c.vm")
> 
> 
> template:c.vm
> ---------------------------->
> #if(                      <-- here a syntaxical error
> 
> 
> The syntax error found in template "c.vm" will not throw a
> "ParseErrorException", I mean, the  "ParseErrorException" will not reach
> the VelocityServlet.
> 
> I also have this problem with the "ResourceNotFoundException" when it
> occurs in a two "levels" template.
> 
> There are no problems with the "MethodInvokationException".
> 
> Thanks in advance,
> 
> Normand
> 
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 

-- 
Geir Magnusson Jr. 
Research & Development, Adeptra Inc.
geirm@adeptra.com
+1-203-247-1713



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


ParseErrorException not thrown...

Posted by Normand Gagnon <ng...@newtradetech.com>.
Hi,

My Servlet extends the VelocityServet (release 1.2) and I override the method:

error(HttpServletRequest request, HttpServletResponse response, Exception ex)

to create different view depending on the occurring exception for web 
designers or programmers.

I noticed that ParseErrorException are not thrown to the VelocityServlet 
when occurring in a "three-level" or more level template, that is, a 
template that was parsed by another template that was also parsed by 
another template.  The error log can be found in the Velocity.log but the 
VelocityServlet's error method is never call ending up with a blank or 
partially parsed template as response.

Exemple:

template:a.vm
------------------------- >
#parse("b.vm")


template:b.vm
--------------------------->
#parse("c.vm")


template:c.vm
---------------------------->
#if(                      <-- here a syntaxical error


The syntax error found in template "c.vm" will not throw a 
"ParseErrorException", I mean, the  "ParseErrorException" will not reach 
the VelocityServlet.

I also have this problem with the "ResourceNotFoundException" when it 
occurs in a two "levels" template.

There are no problems with the "MethodInvokationException".

Thanks in advance,

Normand


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>