You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Andy Cooke <ce...@macs.hw.ac.uk> on 2004/05/23 22:28:53 UTC

accessing $session when using VelocityLayoutServlet

Hi there,

I was wondering how to access HttpSession object from templates served
by the VelocityLayoutServlet.

I tried commands like $session.setAttribute("name", "value"), but got
error messages like:

> $session.setAttribute("name", "value") is not a valid reference.


thanks for the help,

Andy Cooke



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Geir Magnusson Jr <ge...@4quarters.com>.
On Jun 1, 2004, at 10:16 AM, Andy Cooke wrote:
>
>
>> In the velocity area there is also DVSL - which is declarative as 
>> XSLT,
>> but currently (to my knowledge) it is a standalone application and is
>> tied to a specific DOM implementation. Thus not ready to integrate 
>> into
>> a servlet webapp. (Maybe you can take the challenge to change the 
>> model
>> to use plain DOM and JXpath?)
>>    http://jakarta.apache.org/velocity/dvsl/index.html
>
> er, maybe.  I took a brief look at the source code.  Do you mean
> permanently replacing dom4j with dom in the code, or altering code so
> that there could be a choice of models?

Choice please.... :)

When I wrote it I used dom4j because I got a DOM and XPath together, 
but making it so you have a choice would be a good thing.

-- 
Geir Magnusson Jr                                   203-247-1713(m)
geir@4quarters.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Andy Cooke <ce...@macs.hw.ac.uk>.
Hi Christoph,

sorry not to reply earlier on.

> if you are using velocity as the template engine, i suggest you consider
> using anakia (with built in xpath support!) or siply JDOM + JXPath or
> even any DOM parser and Jaxen or JXPath to do the XML query and retrieval
> for the presentation layer. I've used it sucessfully to do online
> transformations.
>    http://jakarta.apache.org/velocity/anakia.html

I wasn't completely sure how anakia might help with on-the-fly xml
manipulation/presentation, as it seems to be closely tied to ant -
unless you mean running ant on the fly?

> In the velocity area there is also DVSL - which is declarative as XSLT,
> but currently (to my knowledge) it is a standalone application and is
> tied to a specific DOM implementation. Thus not ready to integrate into
> a servlet webapp. (Maybe you can take the challenge to change the model
> to use plain DOM and JXpath?)
>    http://jakarta.apache.org/velocity/dvsl/index.html

er, maybe.  I took a brief look at the source code.  Do you mean
permanently replacing dom4j with dom in the code, or altering code so
that there could be a choice of models?

cheers,

Andy


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Geir Magnusson Jr <ge...@4quarters.com>.
On May 27, 2004, at 2:30 AM, Christoph Reck wrote:

>
> In the velocity area there is also DVSL - which is declarative as XSLT,
> but currently (to my knowledge) it is a standalone application and is
> tied to a specific DOM implementation. Thus not ready to integrate into
> a servlet webapp. (Maybe you can take the challenge to change the model
> to use plain DOM and JXpath?)
>   http://jakarta.apache.org/velocity/dvsl/index.html


That would be really cool :)

-- 
Geir Magnusson Jr                                   203-247-1713(m)
geir@4quarters.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Christoph Reck <ap...@recks.org>.
Hi,

Andy Cooke wrote:
> Hi Nathan,
>[snip]
> As for manipulating/ viewing an XML data model - I thought of starting
> off by doing XSLT transformations on the fly.  For updating/storing the
> data, I thought Castor might help...
> 
> cheers,
> 
> Andy Cooke


if you are using velocity as the template engine, i suggest you consider
using anakia (with built in xpath support!) or siply JDOM + JXPath or
even any DOM parser and Jaxen or JXPath to do the XML query and retrieval
for the presentation layer. I've used it sucessfully to do online
transformations.
   http://jakarta.apache.org/velocity/anakia.html

In the velocity area there is also DVSL - which is declarative as XSLT,
but currently (to my knowledge) it is a standalone application and is
tied to a specific DOM implementation. Thus not ready to integrate into
a servlet webapp. (Maybe you can take the challenge to change the model
to use plain DOM and JXpath?)
   http://jakarta.apache.org/velocity/dvsl/index.html

Cheers,
Christoph


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Andy Cooke <ce...@macs.hw.ac.uk>.
Hi Nathan,

> if you're building a small and simple site, then there are smaller and simpler
> frameworks than Struts.  i haven't used it myself, but i've heard good things.
> i'm told it works quite well with VelocityTools (except the Struts tools, of
> course).

I've seen mention of these in some of the other emails - thanks everyone
:-)  I'll check these out.  It's great to know of their existence.

> also, for login validation, you could just use container managed
> authentication or filters, without a full framework.

I'm in a research group at a university.  The website I'm putting
together is pretty simple (5 pages or so), and describes our project to
the public, but which also has an area which only members our our group
can access - this is a place where we can view and update a common set
of information - basically kind of front-end to an XML "database" (that
will be the fun bit).

I followed your suggestion and took a look at "container managed
authentication" - it wouldn't be appropriate for us to set this up, as
we don't administrate the tomcat server.  It looks like I could set up
filters though as this only involves altering web.xml (and implementing
some more servlet methods I think)... but to be honest, I think the
simplest solution for me (I've got this working now, thanks to your
help) is to use a ViewTool with session scope.

As for manipulating/ viewing an XML data model - I thought of starting
off by doing XSLT transformations on the fly.  For updating/storing the
data, I thought Castor might help...

cheers,

Andy Cooke




---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Simple design

Posted by Geir Magnusson Jr <ge...@4quarters.com>.
On May 25, 2004, at 7:41 PM, Barbara Baughman wrote:

> Example of a simple but useful design, for what it's worth.
>
> I have a MAIN controller that extends HttpServlet and gets the
> function name to call from the request parameter "function". The value
> for the function parameter is provided in the request URL or from a
> hidden parameter (INPUT TYPE="hidden" NAME="function").
>

[SNIP]

I have a really hokey little approach that is similar.  I have a base 
class in which the service() method looks for 'action' parameter (like 
your 'function')

It then simply invokes a public method <action>(HttpServletRequest, 
HttpServletResponse, VelocityContext)

it's really embarrassing because it's so simple, but I get tremendous 
mileage out of it.  The base class gets vel setup and has some helper 
methods.  It then makes my servlets look like :

public class Foo extends VelocityActionServlet
{
     public String getTemplateRoot()
     {
         return "foo/";
     }

     public void defaultAction(HttpServletRequest req,
             HttpServletResponse res,
             VelocityContext ctx)
         throws javax.servlet.ServletException, IOException
     {

         ctx.put("entityCount", new Integer(Util.getEntityCount()));
         ctx.put("body_template", "index.vm");
         renderTemplate(req, res, ctx, "frame.vm");
     }
}


The context that's handed to me is created by the baseclass, and I can 
override the creation routine to fill w/ tools, for example, or ignore 
that context.

it's simple and lightweight, but I never have complicated things to 
build with it....


-- 
Geir Magnusson Jr                                   203-247-1713(m)
geir@4quarters.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Simple design

Posted by Barbara Baughman <ba...@utdallas.edu>.
Example of a simple but useful design, for what it's worth.

I have a MAIN controller that extends HttpServlet and gets the
function name to call from the request parameter "function". The value
for the function parameter is provided in the request URL or from a
hidden parameter (INPUT TYPE="hidden" NAME="function").

Each function class implements a WebFunction interface (my invention)
that has an execute method. This method does the appropriate work with
the session and VelocityContext based on the Http parameters, performs
logging, and returns the template name.  The Main controller
instantiates a function class based on the name given in the function
parameter, runs the execute method,and renders the template name
returned.

I like to have the execute method return a $state field that indicates
where the function is in it's life cycle and then render a "function"
template that looks kind of like:

#parse("head.vm")
$setForm  ## The <FORM ACTION=...> line
#set($function="Function")
<INPUT TYPE="hidden" NAME="function" VALUE="$function">
## $state indicates where it is in the function life-cyle
#if ($state=="start")
  #parse("welcome.vm")
#elseif ($state="done")
  #parse("done.vm")
#else
  #parse("welcome.vm")
#end
#parse("tail.vm")

The function's execute method itself does an entire subapplication
that may cover quite a few screens.  Having a function template like
the one above gives the web designer a lot of leeway in handling the
subapplication screen flow as it moves through the various steps.
This is done, of course, entirely with Velocity.

I've been able to do quite a bit with this simple design. These are
web applications used by my University to manage our computer
accounts.  It's not a mega-application, but it's not a hobby either.

Sorry, but there's nothing to see online, since all my applications
require login authentication and all functions require credential
certification.

Barbara Baughman
X2157


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Nathan Bubna <na...@esha.com>.
Andy Cooke said:
...
> > typically, people will control session creation and invalidating
themselves
> > with filters or action classes or through other means.
>
> I'm building a simple site involving a login page and a form, and I
> thought I'd see how far I could get using the VLS before turning to
> things like the struts framework... I'm on a learning curve :-).

if you're building a small and simple site, then there are smaller and simpler
frameworks than Struts.  i haven't used it myself, but i've heard good things.
i'm told it works quite well with VelocityTools (except the Struts tools, of
course).

also, for login validation, you could just use container managed
authentication or filters, without a full framework.

> So I guess I should use a LoginTool for validation and modifying
> templates, rather than trying to mess with the context from a template.

actually, you should feel free to "mess with the context" in the template
(that's what #set() is for!).  i was just saying that it's generally better
not to rely on a template to create/invalidate sessions.

> I've now read the documentation more closely - I guess my LoginTool
> should implement ViewTool and be configured to have "session" scope -
> that way it will get access to the session object (via a ViewSession
> object that is passed when init() is called)... does that sound about
> right?

close, but there's no such thing as a ViewSession.  session scoped tools that
implement the ViewTool interface are passed the current *ViewContext*  when
they are initialized.

> > if you would rather not do this, and want a simple hack to make
> > the VLS create the session automatically, add something like
> > this to your toolbox:
> >
> > <!-- this is the default, but it's here just to be clear -->
> > <create-session>true</create-session>
> > <tool>
> >     <key>tools.with.periods.in.their.key.are.unusable</key>
> >     <scope>session</scope>
> >     <class>java.lang.Object</class>
> > </tool>
> >
> > and that should do the trick.
>
> yes, this, and the #set ($newSession = ...) line did the trick, thanks.
> I'm trying to learn the best ways of using velocity, though :-).
...

actually, you should need to use the $newSession thing if you have done the
hack above.  you can just use $session

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: frameworks, tools, and velocity Was: Re: accessing $session when using VelocityLayoutServlet

Posted by Anthony Eden <ae...@signaturedomains.com>.
What kind of an advocate and project maintainer would I be if I didn't 
mention JPublish? ;-)

JPublish is based on the same design principals as Turbine.  Velocity 
was originally the only template language supported by JPublish, 
although now other template engines such as FreeMarker and WebMacro are 
also supported.  Actions (the logic which provides the glue between your 
view and your business logic) can be written in a variety of scripting 
languages or in Java.  JPublish supports global design templates so that 
designers can work on the site design without interferring with 
programmers or content producers.  Design templates also help reduce the 
amount of work needed to maintain a site design since design elements 
are applied to the site as a whole rather than on each page.

I've used JPublish for projects both large and small and have found that 
it works equally well in either case.  Granted it is not as simple as 
throwing together a small site with PHP, but with the included "starter" 
webapp which you can just expand and modify it is pretty easy to get 
small projects up to speed, and the payoff with the separation of 
concerns is worth it in my opinion.

Anyhow, enough evangelizing.  Give JPublish a try: 
http://www.jpublish.org/ .  It's open source and has a very open license 
(Apache-like).

Sincerely,
Anthony Eden

Nathan Bubna wrote:

>Gonzalo Diethelm said:
>  
>
>...
>  
>
>>* are well integrated with (or depend upon) Velocity
>>    
>>
>
>well, first there is Turbine.  it's *very* well integrated with Velocity, and
>a number of VelocityTools ideas were gleaned from them, though it does not
>itself work with VelocityTools.
>
>then there's Struts w/VelocityStruts.  while the Struts folks have not done
>much themselves to integrate with Velocity, we (VelocityTools) have certainly
>put in the effort to make the two work well together.
>
>there are also several frameworks which seem to have taken steps to integrate
>with Velocity on their own.  these include WebWork and Spring.  i have not
>used WebWork, but i've looked through their code and docs, and it appears
>solid.  my company has recently begun to use Spring with VelocityStruts,
>though i'm not presently working on that project.
>
>the other one i hear good things about is Maverick.  by all accounts it is
>very clean, well-designed framework (though perhaps with less features than
>the rest) that integrates well with pretty much any view layer.  i'm told it
>works great with VelocityTools.
>
>the other major java webapp frameworks i hear about are Tapestry and JSF.
>although JSF--being a spec--could be implemented in Velocity, it has not been,
>and from all i know of Tapestry, i believe that it neither does nor will ever
>have anything to do with Velocity.
>
>  
>
>>Let me rephrase the question under a hypothetical situation: for any of a
>>set of projects that you have to embark on (starting from scratch), please
>>comment on what tools (besides Velocity ;-) you would use. I would be really
>>interested in hearing any opinions for this question; for example:
>>
>>* for a big, enterprise-functionality project: Velocity, foo bar and, of
>>course, baz!
>>    
>>
>
>Turbine, Struts, WebWork, Spring, and Maverick would probably all work for
>these.
>
>  
>
>>* for a small, hobbyist-level project: Velocity, bifur, bofur and, usually,
>>bombur.
>>    
>>
>
>i haven't done this, but i've always figured i'd use VelocityView with
>Velosurf and if i needed a bit more convenience/power/features, then i might
>throw Maverick or parts of Spring into the mix to help out.
>
>  
>
>>* etc.
>>
>>    
>>
>>>actually, i haven't yet had the joy of building any java
>>>webapps small/simple enough to fit this category.
>>>      
>>>
>>And what combinations of tools (in addition to Velocity) have you (and
>>anybody listening) used in real life projects?
>>    
>>
>
>i've used Turbine (with Velocity as view layer), Struts+VelocityStruts, and as
>i said, my coworker has begun working with Spring+Struts+VelocityStruts,
>though i myself have done little work there.
>
>Nathan Bubna
>nathan@esha.com
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: frameworks, tools, and velocity Was: Re: accessing $session when using VelocityLayoutServlet

Posted by "Charles N. Harvey III" <ch...@alloy.com>.

Gonzalo Diethelm said the following on 5/25/2004 10:04 PM:

>>>Would you care to mention some specific frameworks you 
>>>      
>>>
>>recommend? Your 
>>    
>>
>>>answer probably changes from time to time, and depends on what you 
>>>want to do, but anyway I would be interested in frameworks falling 
>>>under these
>>>criteria:
>>>
>>>* are meant to solve a narrow problem, and solve it well (ex: 
>>>velosurf)
>>>      
>>>
>>not sure what you mean here, partly because your example is 
>>not something i would call a true "framework" in this context.
>>    
>>
>
>Sorry, wrong use of the term "framework"; I didn't want to call it "tool",
>didn't want to get it mixed with Velocity tools. What I meant was that
>velosurf is an independent project, solving a very specific problem, and
>able to work with (actually, use) Velocity.
>
>  
>
>>>* are well integrated with (or depend upon) Velocity
>>>      
>>>
>>well, first there is Turbine.  it's *very* well integrated 
>>with Velocity, and a number of VelocityTools ideas were 
>>gleaned from them, though it does not itself work with VelocityTools.
>>    
>>
>
>I have worked on several projects with Turbine. While quite good, I have two
>problems with it: 1) it has overextended itself in functionality; 2) it has
>lost most of its maintainers.
>
>  
>
>>then there's Struts w/VelocityStruts.  while the Struts folks 
>>have not done much themselves to integrate with Velocity, we 
>>(VelocityTools) have certainly put in the effort to make the 
>>two work well together.
>>    
>>
>
>Struts would be closer to what I referred to above: it is intended to solve
>a specific problem (web-based user interface & navigational logic), and
>works well with Velocity. What I want is more examples like velosurf and
>Struts.
>
>  
>
>>there are also several frameworks which seem to have taken 
>>steps to integrate with Velocity on their own.  these include 
>>WebWork and Spring.  i have not used WebWork, but i've looked 
>>through their code and docs, and it appears solid.  my 
>>company has recently begun to use Spring with VelocityStruts, 
>>though i'm not presently working on that project.
>>    
>>
>
>I had heard of both of these, don't know much about them. Are they more
>alike Turbine, or Struts, in terms of the width of its goals?
>  
>

WebWork is just like Struts.  They started out both solving the same problem
that is mentioned above (web-based user interface).  I continue to use 
Struts
because I am familiar with it and I really like the Validator framework it
provides.  WebWork has become much larger and it has also integrated 
with other
tools from OpenSymphony (OSWorkFlow, SiteMesh, OSCache...).  The usual 
deal for
a web framework - xml config file, "action" classes that implement an 
interface,
and the possibility of different view sets (jsp, velocity, freemarker, xml).
http://www.opensymphony.com/webwork/

Spring I am still a little new at (and slightly confused by).  From what 
I can tell
so far it is a more elegant framework that can be used for more than 
just web, but
has a web layer built onto it.  It uses Inversion-of-Control (which I'm 
still not
sure what that means) and integrates well with persistence layers like 
Hibernate.
http://www.springframework.org/

>  
>
>>the other one i hear good things about is Maverick.  by all 
>>accounts it is very clean, well-designed framework (though 
>>perhaps with less features than the rest) that integrates 
>>well with pretty much any view layer.  i'm told it works 
>>great with VelocityTools.
>>    
>>
>
>Ditto.
>  
>

Maverick is actually really cool.  Very small, but very easy.  Same deal 
as WebWork
and Struts but with less "stuff".  No persistence and no validation, 
just web framework,
you provide the rest.  The xml config file is very flexible and you can 
use xml/xsl,
velocity or jsp in the same project.  And you can transform views 
multiple times
to provide wrappers around your documents.  It is definately worth 
looking into if
you have a strictly web-based project.
http://mav.sourceforge.net/

>  
>
>>the other major java webapp frameworks i hear about are 
>>Tapestry and JSF. although JSF--being a spec--could be 
>>implemented in Velocity, it has not been, and from all i know 
>>of Tapestry, i believe that it neither does nor will ever 
>>have anything to do with Velocity.
>>    
>>
>
>I know nothing about these two.
>  
>

JSF - Java Server Faces 
(http://java.sun.com/j2ee/javaserverfaces/index.jsp) is Sun's
attempt to have a web framework.  Haven't really had a look at it so I 
can't say much
about it.  Turbine the same.  Its all smoke and mirrors to me so far.  I 
am pretty
sure it builds your app using fragments of html pages and wraps java 
objects around
them with its extensive configuration elements.  Its supposed to be able 
to do many
of the things EJB does but without all the headaches (I guess - I know 
nothing of EJB).


Hope this helped.


Charlie

>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: frameworks, tools, and velocity Was: Re: accessing $session when using VelocityLayoutServlet

Posted by Gonzalo Diethelm <go...@aditiva.com>.
> > Would you care to mention some specific frameworks you 
> recommend? Your 
> > answer probably changes from time to time, and depends on what you 
> > want to do, but anyway I would be interested in frameworks falling 
> > under these
> > criteria:
> >
> > * are meant to solve a narrow problem, and solve it well (ex: 
> > velosurf)
> 
> not sure what you mean here, partly because your example is 
> not something i would call a true "framework" in this context.

Sorry, wrong use of the term "framework"; I didn't want to call it "tool",
didn't want to get it mixed with Velocity tools. What I meant was that
velosurf is an independent project, solving a very specific problem, and
able to work with (actually, use) Velocity.

> > * are well integrated with (or depend upon) Velocity
> 
> well, first there is Turbine.  it's *very* well integrated 
> with Velocity, and a number of VelocityTools ideas were 
> gleaned from them, though it does not itself work with VelocityTools.

I have worked on several projects with Turbine. While quite good, I have two
problems with it: 1) it has overextended itself in functionality; 2) it has
lost most of its maintainers.

> then there's Struts w/VelocityStruts.  while the Struts folks 
> have not done much themselves to integrate with Velocity, we 
> (VelocityTools) have certainly put in the effort to make the 
> two work well together.

Struts would be closer to what I referred to above: it is intended to solve
a specific problem (web-based user interface & navigational logic), and
works well with Velocity. What I want is more examples like velosurf and
Struts.

> there are also several frameworks which seem to have taken 
> steps to integrate with Velocity on their own.  these include 
> WebWork and Spring.  i have not used WebWork, but i've looked 
> through their code and docs, and it appears solid.  my 
> company has recently begun to use Spring with VelocityStruts, 
> though i'm not presently working on that project.

I had heard of both of these, don't know much about them. Are they more
alike Turbine, or Struts, in terms of the width of its goals?

> the other one i hear good things about is Maverick.  by all 
> accounts it is very clean, well-designed framework (though 
> perhaps with less features than the rest) that integrates 
> well with pretty much any view layer.  i'm told it works 
> great with VelocityTools.

Ditto.

> the other major java webapp frameworks i hear about are 
> Tapestry and JSF. although JSF--being a spec--could be 
> implemented in Velocity, it has not been, and from all i know 
> of Tapestry, i believe that it neither does nor will ever 
> have anything to do with Velocity.

I know nothing about these two.

> > * for a small, hobbyist-level project: Velocity, bifur, bofur and, 
> > usually, bombur.
> 
> i haven't done this, but i've always figured i'd use 
> VelocityView with Velosurf and if i needed a bit more 
> convenience/power/features, then i might throw Maverick or 
> parts of Spring into the mix to help out.

Gotcha. Gotta check these out more thoroughly.

> i've used Turbine (with Velocity as view layer), 
> Struts+VelocityStruts, and as i said, my coworker has begun 
> working with Spring+Struts+VelocityStruts, though i myself 
> have done little work there.

It would be grand if you could convince him/her to write a little note with
his/her thoughts about the Spring+Struts+Velocity combination.

> Nathan Bubna
> nathan@esha.com

Thanks for taking the time.


-- 
Gonzalo Diethelm
gonzalo.diethelm@aditiva.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


frameworks, tools, and velocity Was: Re: accessing $session when using VelocityLayoutServlet

Posted by Nathan Bubna <na...@esha.com>.
Gonzalo Diethelm said:
> Nathan said:
...
> > but again, while Velocity[Tools]-only design is reasonable
> > for small and simple web applications, i would *highly*
> > recommend using an established framework for anything more complex.
>
> Would you care to mention some specific frameworks you recommend? Your
> answer probably changes from time to time, and depends on what you want to
> do, but anyway I would be interested in frameworks falling under these
> criteria:
>
> * are meant to solve a narrow problem, and solve it well (ex: velosurf)

not sure what you mean here, partly because your example is not something i
would call a true "framework" in this context.

> * are well integrated with (or depend upon) Velocity

well, first there is Turbine.  it's *very* well integrated with Velocity, and
a number of VelocityTools ideas were gleaned from them, though it does not
itself work with VelocityTools.

then there's Struts w/VelocityStruts.  while the Struts folks have not done
much themselves to integrate with Velocity, we (VelocityTools) have certainly
put in the effort to make the two work well together.

there are also several frameworks which seem to have taken steps to integrate
with Velocity on their own.  these include WebWork and Spring.  i have not
used WebWork, but i've looked through their code and docs, and it appears
solid.  my company has recently begun to use Spring with VelocityStruts,
though i'm not presently working on that project.

the other one i hear good things about is Maverick.  by all accounts it is
very clean, well-designed framework (though perhaps with less features than
the rest) that integrates well with pretty much any view layer.  i'm told it
works great with VelocityTools.

the other major java webapp frameworks i hear about are Tapestry and JSF.
although JSF--being a spec--could be implemented in Velocity, it has not been,
and from all i know of Tapestry, i believe that it neither does nor will ever
have anything to do with Velocity.

> Let me rephrase the question under a hypothetical situation: for any of a
> set of projects that you have to embark on (starting from scratch), please
> comment on what tools (besides Velocity ;-) you would use. I would be really
> interested in hearing any opinions for this question; for example:
>
> * for a big, enterprise-functionality project: Velocity, foo bar and, of
> course, baz!

Turbine, Struts, WebWork, Spring, and Maverick would probably all work for
these.

> * for a small, hobbyist-level project: Velocity, bifur, bofur and, usually,
> bombur.

i haven't done this, but i've always figured i'd use VelocityView with
Velosurf and if i needed a bit more convenience/power/features, then i might
throw Maverick or parts of Spring into the mix to help out.

> * etc.
>
> > actually, i haven't yet had the joy of building any java
> > webapps small/simple enough to fit this category.
>
> And what combinations of tools (in addition to Velocity) have you (and
> anybody listening) used in real life projects?

i've used Turbine (with Velocity as view layer), Struts+VelocityStruts, and as
i said, my coworker has begun working with Spring+Struts+VelocityStruts,
though i myself have done little work there.

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: accessing $session when using VelocityLayoutServlet

Posted by Gonzalo Diethelm <go...@aditiva.com>.
> > Allow me to interrupt. This is a very (to me) interesting question: 
> > how far can you go in creating a site with pure Velocity 
> (and Java), 
> > without having to use a fuller framework (Turbine, Struts, 
> > what-have-you), but without having to reinvent the wheel?
> 
> heh.  i'd guess that depends on what you mean by "pure 
> Velocity" and "reinventing wheels."  but in general, i'd 
> imagine you _could_ take it pretty far.  i've seen some 
> pretty nasty velocity hacks posted on the dev list in years 
> past.  generally, they were in threads about security issues 
> where an untrusted template author could do insane things 
> (pretty much anything you can imagine).  generally those 
> started with something like:
> 
> #set( $foo = "foo" )$foo.class.forName("java.this.That")
> 
> of course, taking things that far is a Bad Idea (tm).

Yeah, right. But yes, the question is well-meaning, and the kind of code you
mention would certainly NOT fall under any definition of reasonable.

> if i take your question down a more reasonable path, i would 
> say that you could get pretty far with just Velocity, 
> VelocityView (i.e. tool management, the VVS, and the VLS), 
> and some creativity in your tool design.
> 
> one thing that could help you out quite a bit in this vein is 
> Claude's Velosurf tool: http://velosurf.sourceforge.net/

I was aware of this tool, it is a simple and powerful way of bridging
Velocity and an RDBMS.

> but again, while Velocity[Tools]-only design is reasonable 
> for small and simple web applications, i would *highly* 
> recommend using an established framework for anything more complex.

Would you care to mention some specific frameworks you recommend? Your
answer probably changes from time to time, and depends on what you want to
do, but anyway I would be interested in frameworks falling under these
criteria:

* are meant to solve a narrow problem, and solve it well (ex: velosurf)
* are well integrated with (or depend upon) Velocity

Let me rephrase the question under a hypothetical situation: for any of a
set of projects that you have to embark on (starting from scratch), please
comment on what tools (besides Velocity ;-) you would use. I would be really
interested in hearing any opinions for this question; for example:

* for a big, enterprise-functionality project: Velocity, foo bar and, of
course, baz!
* for a small, hobbyist-level project: Velocity, bifur, bofur and, usually,
bombur.
* etc.

> actually, i haven't yet had the joy of building any java 
> webapps small/simple enough to fit this category.

And what combinations of tools (in addition to Velocity) have you (and
anybody listening) used in real life projects?

> Nathan Bubna
> nathan@esha.com

Thanks for your feedback,


-- 
Gonzalo Diethelm
gonzalo.diethelm@aditiva.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Nathan Bubna <na...@esha.com>.
Gonzalo Diethelm said:
> > > typically, people will control session creation and invalidating
> > > themselves with filters or action classes or through other means.
> >
> > I'm building a simple site involving a login page and a form,
> > and I thought I'd see how far I could get using the VLS
> > before turning to things like the struts framework... I'm on
> > a learning curve :-).
>
> Allow me to interrupt. This is a very (to me) interesting question: how far
> can you go in creating a site with pure Velocity (and Java), without having
> to use a fuller framework (Turbine, Struts, what-have-you), but without
> having to reinvent the wheel?

heh.  i'd guess that depends on what you mean by "pure Velocity" and
"reinventing wheels."  but in general, i'd imagine you _could_ take it pretty
far.  i've seen some pretty nasty velocity hacks posted on the dev list in
years past.  generally, they were in threads about security issues where an
untrusted template author could do insane things (pretty much anything you can
imagine).  generally those started with something like:

#set( $foo = "foo" )$foo.class.forName("java.this.That")

of course, taking things that far is a Bad Idea (tm).

if i take your question down a more reasonable path, i would say that you
could get pretty far with just Velocity, VelocityView (i.e. tool management,
the VVS, and the VLS), and some creativity in your tool design.

one thing that could help you out quite a bit in this vein is Claude's
Velosurf tool:
http://velosurf.sourceforge.net/

but again, while Velocity[Tools]-only design is reasonable for small and
simple web applications, i would *highly* recommend using an established
framework for anything more complex.

> Since this is a very vague question, here is
> the more concrete one: can people provide experiences and/or examples of
> sites built ONLY with Velocity, to get a feeling for the power of pure
> Velocity?

actually, i haven't yet had the joy of building any java webapps small/simple
enough to fit this category.

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Christoph Reck <ap...@recks.org>.
Hi, let me jump on this...

Gonzalo Diethelm wrote:
> Allow me to interrupt. This is a very (to me) interesting question: how far
> can you go in creating a site with pure Velocity (and Java), without having
> to use a fuller framework (Turbine, Struts, what-have-you), but without
> having to reinvent the wheel? Since this is a very vague question, here is
> the more concrete one: can people provide experiences and/or examples of
> sites built ONLY with Velocity, to get a feeling for the power of pure
> Velocity?
> 
> Thanks,
> Gonzalo Diethelm

I've done a powerful application with pure velocity prior to VVS/VLS.
Redoing it now with VLS and Vel 1.5 It would even get away without
a ClassUtil in the context (e.g. to create Hastables).

The simple pattern was:
1. at first call to the servlet it loaded an init.vm template
    which pre-loaded a context with some thread safe tools (the
    template output was logged if it had non-whitepace character
    in it - otherwise just discarded).
    This was then used in a chained context for every further request.
2. then before processing the template coded into the URL (the name of
    the service/action), a request.vm template was loaded to pre-process
    the QueryString and PathInfo:
    a) Prepared some per-request variables (e.g. $error, $warning, $log
       $date, $layout="layout.vm"  variables used in the templates and
       the layout).
    b) Ensured that a ACL.xml file was loaded/cached in a context tool.
       The file date was also cached as well as the load time tag, with
       a re-check of the file date after a timepspan. Thus an updated
       ACL file would be transparently reloaded. (Access Control List,
       with a Turbine inspired user-group-role-permission scheme.)
    c) Ensured that a special application configuration file was loaded
       into a JDOM context object (with XPath support), same caching
       pattern as above.
    d) Verified the referenced service was valid, possibly setting
       defaults, and that the user has the right to access it
    e) Upon errors or invalid access, the template to process
       was replaced - e.g. to the login page.
3. The template was processed - normally this was a controller template
    which then parsed in views to create the real HTML page (e.g.
    table.vm, edit.vm, form.vm, etc. where the controller just set
    some well defined context parameters and then parsed the view template).
4. Finally rendered the layout template (the default was overridable by
    one of the previous template.
- all in VTL code!

The application executed actions (VTL templates), read in files, saved
files, executed shell scripts (defined in the application configuration,
with parameters setup from the controller templates), had views
and view fragments parsed-in to display objects, XML data, forms, etc.

The main view was a frameset, some views (e.g. context help where pop-ups).

Since it is used inhouse, I have no site to demostrate it.

I could publish the code, but have no time to give any support. Anyway
it sould be ported to use newer versions of Velocity, VLS, ToolManager,
(J)DOM, and JXPath.

The application framework was coded to shield hackers, but would compromise
the host (at least for the account the servlet container runs on) for
malicious template designers (which applies to any non-java-sandboxed
velocity templates).

Cheers,
Christoph


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: accessing $session when using VelocityLayoutServlet

Posted by Gonzalo Diethelm <go...@aditiva.com>.
> > typically, people will control session creation and invalidating 
> > themselves with filters or action classes or through other means.
> 
> I'm building a simple site involving a login page and a form, 
> and I thought I'd see how far I could get using the VLS 
> before turning to things like the struts framework... I'm on 
> a learning curve :-).

Allow me to interrupt. This is a very (to me) interesting question: how far
can you go in creating a site with pure Velocity (and Java), without having
to use a fuller framework (Turbine, Struts, what-have-you), but without
having to reinvent the wheel? Since this is a very vague question, here is
the more concrete one: can people provide experiences and/or examples of
sites built ONLY with Velocity, to get a feeling for the power of pure
Velocity?

Thanks,


-- 
Gonzalo Diethelm
gonzalo.diethelm@aditiva.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Andy Cooke <ce...@macs.hw.ac.uk>.
Hi Nathan,

> if you are creating and using the session in the template, then on the first
> request you will need to set it to a unique key:
> 
> #set( $newSession = $request.getSession(true) )

yes, that worked... thanks!

> typically, people will control session creation and invalidating themselves
> with filters or action classes or through other means.

I'm building a simple site involving a login page and a form, and I
thought I'd see how far I could get using the VLS before turning to
things like the struts framework... I'm on a learning curve :-).

So I guess I should use a LoginTool for validation and modifying
templates, rather than trying to mess with the context from a template.
I've now read the documentation more closely - I guess my LoginTool
should implement ViewTool and be configured to have "session" scope -
that way it will get access to the session object (via a ViewSession
object that is passed when init() is called)... does that sound about
right?

> if you would rather not do this, and want a simple hack to make the VLS create
> the session automatically, add something like this to your toolbox:
> 
> <!-- this is the default, but it's here just to be clear -->
> <create-session>true</create-session>
> <tool>
>     <key>tools.with.periods.in.their.key.are.unusable</key>
>     <scope>session</scope>
>     <class>java.lang.Object</class>
> </tool>
> 
> and that should do the trick.

yes, this, and the #set ($newSession = ...) line did the trick, thanks. 
I'm trying to learn the best ways of using velocity, though :-).

> if you
> do want to learn more about it, then i'd suggest looking at VelocityTools'
> ChainedContext class.  it has more to do with that than with template parsing.
> :)

I took a look, and it helped - thanks.

Andy


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Nathan Bubna <na...@esha.com>.
Andy Cooke said:
> Nathan said:
> > Velocity may just be complaining about it in the logs because that method
> > returns void.  when you know a method returns void or null, you should use
> > quiet notation.
>
> > $!session.setAttribute('foo','bar')
>
> ok, thanks for pointing that out.
>
> > if that still doesn't work, try calling $request.getSession(true) before
that
> > to make sure that a session has been created.
>
> OK, I've experimented with this, and here are the results (sorry, the
> email is a little long). If my template.vm has:
>
>     $request.getSession(true)<br/>
>     $session.isNew()<br/>
>     $!session.setAttribute("foo", "bar")<br/>
>     $session.getAttribute("foo")<br/>
>
> then the first time I access template.vm, I see the following in the
> browser:
>
>     org.apache.catalina.session.StandardSessionFacade@f8395f
>     $session.isNew()
>     $session.getAttribute("foo")

d'oh.  sorry, i was forgetting how the ChainedContext worked there...

if you are creating and using the session in the template, then on the first
request you will need to set it to a unique key:

#set( $newSession = $request.getSession(true) )

then you can just use that.  or you can continue to use:

$request.getSession(true).isNew()
$request.getSession(true).setAttribute('foo','bar')

and so on...

but, with that said, i don't recommend depending on templates for creating
your sessions.  (more on this below)

> But on a second access, I'll see what I'd expect:
>
>     org.apache.catalina.session.StandardSessionFacade@4bf53e
>     false
>     bar
>
> Repeating the same experiment, after removing the "!", I'll also see the
> line "$session.setAttribute("foo", "bar") the first time, but not the
> next time.
>
> Lastly, commenting out the line with "$request.getSession(true)" seems
> to break the code.  I'll get:
>
>     $session.isNew()
>     $session.setAttribute("foo" "bar")
>     $session.getAttribute("foo")
>
> no matter how many times I access the page.  I imagined that
> VelocityLayoutServlet would create the session for me, but I guess that
> isn't the case. (What about the other servlets?.)

no, the only case where the VelocityLayoutServlet or VelocityViewServlet will
create a session for you is if your toolbox definition includes a session
scoped tool.  e.g.

<toolbox>
<tool>
    <key>user</key>
    <scope>session</scope>
    <class>com.my.UserTool</class>
</tool>
</toolbox>

and even then, you can tell the VLS and VVS not to do this by adding

<create-session>false</create-session> to the toolbox.

typically, people will control session creation and invalidating themselves
with filters or action classes or through other means.

if you would rather not do this, and want a simple hack to make the VLS create
the session automatically, add something like this to your toolbox:

<!-- this is the default, but it's here just to be clear -->
<create-session>true</create-session>
<tool>
    <key>tools.with.periods.in.their.key.are.unusable</key>
    <scope>session</scope>
    <class>java.lang.Object</class>
</tool>

and that should do the trick.

> I was also a bit
> miffed that the first time I access the template, I don't see what I
> would hope - I guess I need to learn a little about the way in which the
> template is parsed?

well, that's partly because i gave you a poor solution originally, but if you
do want to learn more about it, then i'd suggest looking at VelocityTools'
ChainedContext class.  it has more to do with that than with template parsing.
:)

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Andy Cooke <ce...@macs.hw.ac.uk>.
Hi Nathan,

thanks for the reply.

> Velocity may just be complaining about it in the logs because that method
> returns void.  when you know a method returns void or null, you should use
> quiet notation.

> $!session.setAttribute('foo','bar')

ok, thanks for pointing that out.

> if that still doesn't work, try calling $request.getSession(true) before that
> to make sure that a session has been created.

OK, I've experimented with this, and here are the results (sorry, the
email is a little long). If my template.vm has:

    $request.getSession(true)<br/>
    $session.isNew()<br/>
    $!session.setAttribute("foo", "bar")<br/>
    $session.getAttribute("foo")<br/>

then the first time I access template.vm, I see the following in the
browser:

    org.apache.catalina.session.StandardSessionFacade@f8395f
    $session.isNew()
    $session.getAttribute("foo")

But on a second access, I'll see what I'd expect:

    org.apache.catalina.session.StandardSessionFacade@4bf53e
    false
    bar

Repeating the same experiment, after removing the "!", I'll also see the
line "$session.setAttribute("foo", "bar") the first time, but not the
next time.

Lastly, commenting out the line with "$request.getSession(true)" seems
to break the code.  I'll get:

    $session.isNew()
    $session.setAttribute("foo" "bar")
    $session.getAttribute("foo")

no matter how many times I access the page.  I imagined that
VelocityLayoutServlet would create the session for me, but I guess that
isn't the case. (What about the other servlets?.)  I was also a bit
miffed that the first time I access the template, I don't see what I
would hope - I guess I need to learn a little about the way in which the
template is parsed?

cheers,

Andy





--


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: accessing $session when using VelocityLayoutServlet

Posted by Nathan Bubna <na...@esha.com>.
Andy Cooke said:
> I was wondering how to access HttpSession object from templates served
> by the VelocityLayoutServlet.
>
> I tried commands like $session.setAttribute("name", "value"), but got
> error messages like:
>
> > $session.setAttribute("name", "value") is not a valid reference.

are you sure that the call to $session.setAttribute("foo","bar") isn't
working?
Velocity may just be complaining about it in the logs because that method
returns void.  when you know a method returns void or null, you should use
quiet notation.

$!session.setAttribute('foo','bar')

if that still doesn't work, try calling $request.getSession(true) before that
to make sure that a session has been created.

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org