You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by otisg <ot...@ureach.com> on 2002/10/18 07:21:18 UTC

VelocityStruts and using HttpSession as Velocity Context

Hello,

I've got a question about the VelocityStruts
sub-project.
Most examples (app1/2/3) use msg, form,
error, link, request, etc. context keys. 
While this is fine, this is not what the
developer needs most of the time.  Most of
the time one has to stick custom object with
custom keys in the context.

After looking at the 3 examples I found
$user in app3 example.  This is the custom
context key.  Then I went to the app3 .java
files to see how that 'user' key made it
into the Velocity context.

The only thing that I could find is this, in
some *Action class' perform method:

  HttpSession session = request.getSession();
  session.setAttribute(Constants.USER_KEY,
form);
  // Note: Constants.USER_KEY is set to "user"

Is this really how Action classes and VM
templates communicate?
Via the HttpSession?
Is this a good thing?
I always thought and read that one has to be
careful not to put to much data in the
session, as that will result in increased
memory consumption when lots of users are
using the system simultaneously.

I used WebMacro a few years ago, just around
the time when Velocity was created. 
WebMacro had a notion of a WebContext, which
was like a HashMap with String keys and
values that could hold any kind of an object.
Velocity has a VelocityContext, or something
similar, I believe.

Isn't this how data should ideally be passed
between a servlet/Action class and a VM
template?

Is this just something that cannot be
achieved with Struts?

Thanks,
Otis


________________________________________________
Get your own "800" number
Voicemail, fax, email, and a lot more
http://www.ureach.com/reg/tag

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


Re: VelocityStruts and using HttpSession as Velocity Context

Posted by Gabriel Sidler <si...@teamup.com>.
Otis,
Tim explains it very well. I'd just like to add one thing:
The main reason for using the three servlet contexts to
pass data between the model and the view is that it creates
a clean separation between the model and view. You can use
any servlet-based view technology and do not need to change
the model and control part (Struts actions) at all.
Some of the provided examples show this by allowing to switch
between Velocity templates and JSP pages on the fly. Also,
it is easily possible to create apps that mix several view
technologies, easing the transition between view technologies.

The VelocityViewServlet, which is at the core of the Velocity/
Struts integration, uses a special context implementation.
If you access a variable within the template and it is not found
in the context itself, the context object transparently tries
to find it in the request context, session context and application
context of the servlet (in that order). This gives the template
writer very convenient access to all objects that are contained
in one of the servlet contexts.


Gabe




Tim Colson wrote:

> Otis -
> 
> 
>>Most examples (app1/2/3) use msg, form,
>>error, link, request, etc. context keys. 
>>While this is fine, this is not what the
>>developer needs most of the time.  
>>
> 
> Right, those are examples of using the Struts tools that are
> automagically dropped into the Velocity context. (Gabriel had to do a
> lot more work on those, so they probably got higher billing in the
> examples. :-)
> 
> 
>>Most of the time one has to stick custom object with
>>custom keys in the context.
>>
> I understand - But it's amazingly simple to do this... wait a sec and
> I'll show you.
> 
> 
>>The only thing that I could find is this, in
>>some *Action class' perform method:
>>Is this really how Action classes and VM
>>templates communicate?
>>Via the HttpSession?
>>
> 
> Well, yes and no. The action classes can place objects into the Request,
> the Session or Application scopes and the VelocityServlet will
> automatically search through all of them to find a valid reference.
> 
> For example: the action could do: request.setAttribute("widget", new
> Widget("foo"));
> 
> And then in the template vm, you can write $widget.name and it will be
> found. (assuming Widget objects have a public method for getName() or
> get("name") of course... HashMap objects work nicely for many
> ViewObjects).
> 
> 
>>I always thought and read that one has to be
>>careful not to put to much data in the
>>session, as that will result in increased
>>memory consumption when lots of users are
>>using the system simultaneously.
>>
> 
> That is correct. But this is true of any web application. Perhaps it is
> less confusing now that you see how all three scopes can be used? 
> 
> I had a look, and the scopes are mentioned briefly in the docs under the
> heading "Passing Application Data" ... but the docs are a bit thin
> still. (I'll offer to help Gabriel on this again, perhaps I will write
> some  more info and submit it to him for inclusion.)
> 
> 
>>WebMacro had a notion of a WebContext, which
>>was like a HashMap with String keys and
>>values that could hold any kind of an object.
>>Velocity has a VelocityContext, or something
>>similar, I believe.
>>
> 
> Correct. The VelocityContext is pretty much as you describe. 
> 
> 
>>Isn't this how data should ideally be passed
>>between a servlet/Action class and a VM
>>template?
>>
> 
> Ideally, I think, yes. However, in order to make this integration work
> easily, and allow Struts developers to mix and match JSP and Velocity,
> it was decided that it would be better for VelocityServlet to search the
> request/session/app scopes. 
> 
> 
>>Is this just something that cannot be
>>achieved with Struts?
>>
> It can, AFAIK. But the work was done to make it relatively easy to make
> VM's a drop-in replacement for JSP's in a Struts app. 
> 
> I've been doing this for a while in some internal apps and I've had
> great success with it. Legacy views continue to use JSP, but new actions
> use Velocity templates for the view.
> 
> I have had some problems where the way we put data into the web context
> for JSP's isn't as efficient now that I have VM's which make it easier
> to get to nested data structures. (I somewhat mistakenly call the three
> scopes the "web context", but it's easier to say compared to
> "request/session/application scopes" <grin>). 
> 
> Overall, VM has helped make my Struts apps much easier to build versus
> the JSP and custom tag libraries. :-)
> 
> Cheers,
> Timo
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 


-- 
--
Gabriel Sidler
Software Engineer, Eivycom GmbH, Zurich, Switzerland


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


RE: VelocityStruts and using HttpSession as Velocity Context

Posted by Tim Colson <tc...@cisco.com>.
Otis -

> Most examples (app1/2/3) use msg, form,
> error, link, request, etc. context keys. 
> While this is fine, this is not what the
> developer needs most of the time.  

Right, those are examples of using the Struts tools that are
automagically dropped into the Velocity context. (Gabriel had to do a
lot more work on those, so they probably got higher billing in the
examples. :-)

> Most of the time one has to stick custom object with
> custom keys in the context.
I understand - But it's amazingly simple to do this... wait a sec and
I'll show you.

> The only thing that I could find is this, in
> some *Action class' perform method:
> Is this really how Action classes and VM
> templates communicate?
> Via the HttpSession?

Well, yes and no. The action classes can place objects into the Request,
the Session or Application scopes and the VelocityServlet will
automatically search through all of them to find a valid reference.

For example: the action could do: request.setAttribute("widget", new
Widget("foo"));

And then in the template vm, you can write $widget.name and it will be
found. (assuming Widget objects have a public method for getName() or
get("name") of course... HashMap objects work nicely for many
ViewObjects).

> I always thought and read that one has to be
> careful not to put to much data in the
> session, as that will result in increased
> memory consumption when lots of users are
> using the system simultaneously.

That is correct. But this is true of any web application. Perhaps it is
less confusing now that you see how all three scopes can be used? 

I had a look, and the scopes are mentioned briefly in the docs under the
heading "Passing Application Data" ... but the docs are a bit thin
still. (I'll offer to help Gabriel on this again, perhaps I will write
some  more info and submit it to him for inclusion.)

> WebMacro had a notion of a WebContext, which
> was like a HashMap with String keys and
> values that could hold any kind of an object.
> Velocity has a VelocityContext, or something
> similar, I believe.

Correct. The VelocityContext is pretty much as you describe. 

> Isn't this how data should ideally be passed
> between a servlet/Action class and a VM
> template?

Ideally, I think, yes. However, in order to make this integration work
easily, and allow Struts developers to mix and match JSP and Velocity,
it was decided that it would be better for VelocityServlet to search the
request/session/app scopes. 

> Is this just something that cannot be
> achieved with Struts?
It can, AFAIK. But the work was done to make it relatively easy to make
VM's a drop-in replacement for JSP's in a Struts app. 

I've been doing this for a while in some internal apps and I've had
great success with it. Legacy views continue to use JSP, but new actions
use Velocity templates for the view.

I have had some problems where the way we put data into the web context
for JSP's isn't as efficient now that I have VM's which make it easier
to get to nested data structures. (I somewhat mistakenly call the three
scopes the "web context", but it's easier to say compared to
"request/session/application scopes" <grin>). 

Overall, VM has helped make my Struts apps much easier to build versus
the JSP and custom tag libraries. :-)

Cheers,
Timo


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