You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Dan Bachelder <ch...@chowda.net> on 2001/07/31 01:34:14 UTC

design patterns

I'm rather new to the velocity scene...

I was kind of wondering about how people design to use the velocity in their
web apps?

do you use a servlet to sit in front and serve all the pages? do you design
modules based on like-content? do you use a page-centric view? or something
completely different?

discussion or resources are welcome... thanks.

dan


Re: design patterns

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Nick Bauman wrote:
> 
> I wrote a servlet that maps two extensions: ".vtl" and ".x".
> 
> When "*.x" files are called, the servlet sends the request to a Command
> Factory, which finds (Sometimes loads, ie, lazy instantiation) the correct
> Controller, which takes in an argument of the HttpServletRequest,
> HttpServletResponse and a VelocityContext; which does the interaction with
> an SQL O/R mapping layer to the backend (Model). It then populates the
> Template Context with the correct "beans" (velocity symbols, in this case)
> and then merges it with a predefined view for that Controller.
> 
> So a call to this URL:
> 
> http://whatever.com/Login.x
> 
> goes into the controller:
> 
> com.whatever.servlet.controller.Login

How do you control then what they user does?  Does your mapper have any
sense of 'application context', knowing what a user can and cannot
invoke?

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Well done is better than well said - New England Proverb

Re: design patterns

Posted by Nick Bauman <ni...@cortexity.com>.
I wrote a servlet that maps two extensions: ".vtl" and ".x".

When "*.x" files are called, the servlet sends the request to a Command 
Factory, which finds (Sometimes loads, ie, lazy instantiation) the correct 
Controller, which takes in an argument of the HttpServletRequest, 
HttpServletResponse and a VelocityContext; which does the interaction with 
an SQL O/R mapping layer to the backend (Model). It then populates the 
Template Context with the correct "beans" (velocity symbols, in this case) 
and then merges it with a predefined view for that Controller.

So a call to this URL:

http://whatever.com/Login.x

goes into the controller:

com.whatever.servlet.controller.Login

via the Command Factory, which then populates the context with the correct 
bean-like objects and then merges with the template called:

http://whatever.com/LoginView.vtl

That's how I do it.

> Model2 +1 design pattern :)
> 
> however, I am not very interested in going down the turbine path right
> now... I have been a EJB developer for a while now.. so I am going to
> jump in and do some servlet stuff... however the idea of coding HTML
> directly into my java code gives me the hebbie-jebbies (sp?)... So I
> figured, why not try that velocity thing out :)... but now I am hoping
> I can build on the experience of other velocity developers and learn
> what has worked (and not worked) for people on past and current
> projects that involve velocity. When I said "design pattern" I wasn't
> really talking about a GoF style pattern.. a bit more concrete than
> that I guess...
> 
> ----- Original Message -----
> From: "Nick Bauman" <ni...@cortexity.com>
> To: <ve...@jakarta.apache.org>
> Sent: Tuesday, July 31, 2001 12:01 AM
> Subject: Re: design patterns
> 
> 
>> You can use MVC with Turbine out of the box, I've heard.
>>
>> > I'm rather new to the velocity scene...
>> >
>> > I was kind of wondering about how people design to use the velocity
>> > in their web apps?
>> >
>> > do you use a servlet to sit in front and serve all the pages? do you
>> > design modules based on like-content? do you use a page-centric
>> > view? or something completely different?
>> >
>> > discussion or resources are welcome... thanks.
>> >
>> > dan
>>
>>
>> --
>> Nick Bauman
>> 3600 N. Dupont
>> Minneapolis, MN 55412
>> Mobile: (612) 232-7120
>> SMS: 6122327120@voicestream.net
>> Home: (612) 522-0165


-- 
Nick Bauman
3600 N. Dupont
Minneapolis, MN 55412
Mobile: (612) 232-7120
SMS: 6122327120@voicestream.net
Home: (612) 522-0165


Re: including macro definitions

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Paul Sorenson wrote:
> 
> Geir - thanks, you also answered my next question, was this capability
> coming.
> The velocimacro.library approach will get me by until then.
> 
> Any tips on when is this capability is likely to become available?
> 

I am working on the VM stuff at the moment, trying to make autoloading
work for libraries so that you don't have to do anything special (like
dump the servlet runner) to get the VM libraries to auto-reload when you
make a change...  I will see what it takes to stuff this in too.  It's
not as trivial as it first appears.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Well done is better than well said - New England Proverb

Re: including macro definitions

Posted by Paul Sorenson <pa...@classware.com.au>.
Geir - thanks, you also answered my next question, was this capability
coming.
The velocimacro.library approach will get me by until then.

Any tips on when is this capability is likely to become available?

Cheers

----- Original Message -----
From: "Geir Magnusson Jr." <ge...@optonline.net>
To: <ve...@jakarta.apache.org>
Sent: Monday, August 13, 2001 9:15 PM
Subject: Re: including macro definitions


> Sort of.  The problem is that #parse() is a runtime thing, and the
> decision about weather #<foo> is a VM is a parse-time thing, so you
> really can't #parse() in a set of VMs right now (it's coming...)
>
> However, the VM system supports having any number of VM libraries that
> will be loaded at startup :
>
> velocimacro.library = lib1.vm, lib2.vm, lib3.vm, etc



Re: including macro definitions

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Paul Sorenson wrote:
> 
> Would it be true to say that "macros defined inside a file A that is #parsed
> in file B are not available to file B" (regardless of the value of
> velocimacro.permissions.allow.inline.local.scope and )?

Sort of.  The problem is that #parse() is a runtime thing, and the
decision about weather #<foo> is a VM is a parse-time thing, so you
really can't #parse() in a set of VMs right now (it's coming...)

However, the VM system supports having any number of VM libraries that
will be loaded at startup :

velocimacro.library = lib1.vm, lib2.vm, lib3.vm, etc

> 
> I have tried this myself and also dredged through the archive and am
> guessing the answer is yes but it would be nice if that could be confirmed.

:)

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Well done is better than well said - New England Proverb

including macro definitions

Posted by Paul Sorenson <pa...@classware.com.au>.
Would it be true to say that "macros defined inside a file A that is #parsed
in file B are not available to file B" (regardless of the value of
velocimacro.permissions.allow.inline.local.scope and )?

I have tried this myself and also dredged through the archive and am
guessing the answer is yes but it would be nice if that could be confirmed.



Re: Composition

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Did you ever figure this out?

Kelvin Tan wrote:
> 
> Hi,
> 
> I'm having a little trouble with Composition here, so hope someone can shed
> some light on the issue.
> 
> I've a Feature class which contains(amongst others) a reason String and a
> Priority object. They are not assigned to any particular object, with only
> their references declared.
> 
> In the template, if I use
> 
> $!feature.reason
> 
> nothing is displayed (which is correct). If I assign a value to "reason",
> the value is displayed.
> 
> However, if I'm use
> 
> $!feature.priority or $feature.getPriority() or
> 
> #set($priority = $feature.priority)
> $!priority
> 
> I get a
> 
> org.apache.velocity.exception.MethodInvocationException: Invocation of
> method 'getPriority' in class Feature threw exception class
> java.lang.NullPointerException
> 
> Did I miss any steps out which I should've? Would appreciate the help. Am
> using Velocity v1.1. Class listings are below. Thanks!
> 
> public class Feature
> {
>  Priority priority;
>  String reason;
> ...
>  public String getPriority()
>  {
>   return priority.getPriority();
>  }
> }
> 
> public class Priority
> {
> private String state;
>  public String getPriority()
>  {
>   return state;
>  }
> }

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Well done is better than well said - New England Proverb

Re: Composition

Posted by Kelvin Tan <ke...@relevanz.com>.
hmmmm...yes you're right. Clearly don't have sufficiently strong concept. I
thought that if you didn't initialize a variable, it'd be void actually.

Thanks anyway...appreciate the pointer.

----- Original Message -----
From: "Jim Seach" <jw...@yahoo.com>
To: <ve...@jakarta.apache.org>
Sent: Tuesday, July 31, 2001 7:39 PM
Subject: Re: Composition


> Shouldn't your feature.getPriority be:
>
>   public String getPriority()
>   {
>    if (priority == null)
>      return null;
>    else
>      return priority.getPriority();
>   }
>
> Jim
>
> --- Kelvin Tan <ke...@myangel.com> wrote:
> > Hi,
> >
> > I'm having a little trouble with Composition here,
> > so hope someone can shed
> > some light on the issue.
> >
> > I've a Feature class which contains(amongst others)
> > a reason String and a
> > Priority object. They are not assigned to any
> > particular object, with only
> > their references declared.
> >
> > In the template, if I use
> >
> > $!feature.reason
> >
> > nothing is displayed (which is correct). If I assign
> > a value to "reason",
> > the value is displayed.
> >
> > However, if I'm use
> >
> > $!feature.priority or $feature.getPriority() or
> >
> > #set($priority = $feature.priority)
> > $!priority
> >
> > I get a
> >
> >
> org.apache.velocity.exception.MethodInvocationException:
> > Invocation of
> > method 'getPriority' in class Feature threw
> > exception class
> > java.lang.NullPointerException
> >
> > Did I miss any steps out which I should've? Would
> > appreciate the help. Am
> > using Velocity v1.1. Class listings are below.
> > Thanks!
> >
> > public class Feature
> > {
> >  Priority priority;
> >  String reason;
> > ...
> >  public String getPriority()
> >  {
> >   return priority.getPriority();
> >  }
> > }
> >
> > public class Priority
> > {
> > private String state;
> >  public String getPriority()
> >  {
> >   return state;
> >  }
> > }
> >
>
>
> __________________________________________________
> Do You Yahoo!?
> Make international calls for as low as $.04/minute with Yahoo! Messenger
> http://phonecard.yahoo.com/
>


Re: Composition

Posted by Jim Seach <jw...@yahoo.com>.
Shouldn't your feature.getPriority be:

  public String getPriority()
  {
   if (priority == null)
     return null;
   else
     return priority.getPriority();
  }

Jim

--- Kelvin Tan <ke...@myangel.com> wrote:
> Hi,
> 
> I'm having a little trouble with Composition here,
> so hope someone can shed
> some light on the issue.
> 
> I've a Feature class which contains(amongst others)
> a reason String and a
> Priority object. They are not assigned to any
> particular object, with only
> their references declared.
> 
> In the template, if I use
> 
> $!feature.reason
> 
> nothing is displayed (which is correct). If I assign
> a value to "reason",
> the value is displayed.
> 
> However, if I'm use
> 
> $!feature.priority or $feature.getPriority() or
> 
> #set($priority = $feature.priority)
> $!priority
> 
> I get a
> 
>
org.apache.velocity.exception.MethodInvocationException:
> Invocation of
> method 'getPriority' in class Feature threw
> exception class
> java.lang.NullPointerException
> 
> Did I miss any steps out which I should've? Would
> appreciate the help. Am
> using Velocity v1.1. Class listings are below.
> Thanks!
> 
> public class Feature
> {
>  Priority priority;
>  String reason;
> ...
>  public String getPriority()
>  {
>   return priority.getPriority();
>  }
> }
> 
> public class Priority
> {
> private String state;
>  public String getPriority()
>  {
>   return state;
>  }
> }
> 


__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

Composition

Posted by Kelvin Tan <ke...@myangel.com>.
Hi,

I'm having a little trouble with Composition here, so hope someone can shed
some light on the issue.

I've a Feature class which contains(amongst others) a reason String and a
Priority object. They are not assigned to any particular object, with only
their references declared.

In the template, if I use

$!feature.reason

nothing is displayed (which is correct). If I assign a value to "reason",
the value is displayed.

However, if I'm use

$!feature.priority or $feature.getPriority() or

#set($priority = $feature.priority)
$!priority

I get a

org.apache.velocity.exception.MethodInvocationException: Invocation of
method 'getPriority' in class Feature threw exception class
java.lang.NullPointerException

Did I miss any steps out which I should've? Would appreciate the help. Am
using Velocity v1.1. Class listings are below. Thanks!

public class Feature
{
 Priority priority;
 String reason;
...
 public String getPriority()
 {
  return priority.getPriority();
 }
}

public class Priority
{
private String state;
 public String getPriority()
 {
  return state;
 }
}


Re: design patterns

Posted by Dan Bachelder <ch...@chowda.net>.
I wasn't talking about velocity developers (like geir)... I was talking
about developers who use velocity on current or old products... I would like
to understand how to use velocity and servlets work before I add another
layer... I'm sure I'll learn turbine in the future...
thanks.

----- Original Message -----
From: "Jon Stevens" <jo...@latchkey.com>
To: <ve...@jakarta.apache.org>
Sent: Monday, July 30, 2001 11:42 PM
Subject: Re: design patterns


>
> You say you want to learn from the velocity developers...then look at
> Turbine...because the velocity developers also work on Turbine (except
> geir)...
>
> -jon
>
> On Mon, 30 Jul 2001, Dan Bachelder wrote:
>
> > Model2 +1 design pattern :)
> >
> > however, I am not very interested in going down the turbine path right
> > now... I have been a EJB developer for a while now.. so I am going to
jump
> > in and do some servlet stuff... however the idea of coding HTML directly
> > into my java code gives me the hebbie-jebbies (sp?)... So I figured, why
not
> > try that velocity thing out :)... but now I am hoping I can build on the
> > experience of other velocity developers and learn what has worked (and
not
> > worked) for people on past and current projects that involve velocity.
When
> > I said "design pattern" I wasn't really talking about a GoF style
pattern..
> > a bit more concrete than that I guess...
> >
> > ----- Original Message -----
> > From: "Nick Bauman" <ni...@cortexity.com>
> > To: <ve...@jakarta.apache.org>
> > Sent: Tuesday, July 31, 2001 12:01 AM
> > Subject: Re: design patterns
> >
> >
> > > You can use MVC with Turbine out of the box, I've heard.
> > >
> > > > I'm rather new to the velocity scene...
> > > >
> > > > I was kind of wondering about how people design to use the velocity
in
> > > > their web apps?
> > > >
> > > > do you use a servlet to sit in front and serve all the pages? do you
> > > > design modules based on like-content? do you use a page-centric
view?
> > > > or something completely different?
> > > >
> > > > discussion or resources are welcome... thanks.
> > > >
> > > > dan
> > >
> > >
> > > --
> > > Nick Bauman
> > > 3600 N. Dupont
> > > Minneapolis, MN 55412
> > > Mobile: (612) 232-7120
> > > SMS: 6122327120@voicestream.net
> > > Home: (612) 522-0165
> > >
> > >
> >
>
>


Re: design patterns

Posted by Jon Stevens <jo...@latchkey.com>.
You say you want to learn from the velocity developers...then look at
Turbine...because the velocity developers also work on Turbine (except
geir)...

-jon

On Mon, 30 Jul 2001, Dan Bachelder wrote:

> Model2 +1 design pattern :)
>
> however, I am not very interested in going down the turbine path right
> now... I have been a EJB developer for a while now.. so I am going to jump
> in and do some servlet stuff... however the idea of coding HTML directly
> into my java code gives me the hebbie-jebbies (sp?)... So I figured, why not
> try that velocity thing out :)... but now I am hoping I can build on the
> experience of other velocity developers and learn what has worked (and not
> worked) for people on past and current projects that involve velocity. When
> I said "design pattern" I wasn't really talking about a GoF style pattern..
> a bit more concrete than that I guess...
>
> ----- Original Message -----
> From: "Nick Bauman" <ni...@cortexity.com>
> To: <ve...@jakarta.apache.org>
> Sent: Tuesday, July 31, 2001 12:01 AM
> Subject: Re: design patterns
>
>
> > You can use MVC with Turbine out of the box, I've heard.
> >
> > > I'm rather new to the velocity scene...
> > >
> > > I was kind of wondering about how people design to use the velocity in
> > > their web apps?
> > >
> > > do you use a servlet to sit in front and serve all the pages? do you
> > > design modules based on like-content? do you use a page-centric view?
> > > or something completely different?
> > >
> > > discussion or resources are welcome... thanks.
> > >
> > > dan
> >
> >
> > --
> > Nick Bauman
> > 3600 N. Dupont
> > Minneapolis, MN 55412
> > Mobile: (612) 232-7120
> > SMS: 6122327120@voicestream.net
> > Home: (612) 522-0165
> >
> >
>


Re: design patterns

Posted by Dan Bachelder <ch...@chowda.net>.
Model2 +1 design pattern :)

however, I am not very interested in going down the turbine path right
now... I have been a EJB developer for a while now.. so I am going to jump
in and do some servlet stuff... however the idea of coding HTML directly
into my java code gives me the hebbie-jebbies (sp?)... So I figured, why not
try that velocity thing out :)... but now I am hoping I can build on the
experience of other velocity developers and learn what has worked (and not
worked) for people on past and current projects that involve velocity. When
I said "design pattern" I wasn't really talking about a GoF style pattern..
a bit more concrete than that I guess...

----- Original Message -----
From: "Nick Bauman" <ni...@cortexity.com>
To: <ve...@jakarta.apache.org>
Sent: Tuesday, July 31, 2001 12:01 AM
Subject: Re: design patterns


> You can use MVC with Turbine out of the box, I've heard.
>
> > I'm rather new to the velocity scene...
> >
> > I was kind of wondering about how people design to use the velocity in
> > their web apps?
> >
> > do you use a servlet to sit in front and serve all the pages? do you
> > design modules based on like-content? do you use a page-centric view?
> > or something completely different?
> >
> > discussion or resources are welcome... thanks.
> >
> > dan
>
>
> --
> Nick Bauman
> 3600 N. Dupont
> Minneapolis, MN 55412
> Mobile: (612) 232-7120
> SMS: 6122327120@voicestream.net
> Home: (612) 522-0165
>
>


Re: design patterns

Posted by Nick Bauman <ni...@cortexity.com>.
You can use MVC with Turbine out of the box, I've heard.

> I'm rather new to the velocity scene...
> 
> I was kind of wondering about how people design to use the velocity in
> their web apps?
> 
> do you use a servlet to sit in front and serve all the pages? do you
> design modules based on like-content? do you use a page-centric view?
> or something completely different?
> 
> discussion or resources are welcome... thanks.
> 
> dan


-- 
Nick Bauman
3600 N. Dupont
Minneapolis, MN 55412
Mobile: (612) 232-7120
SMS: 6122327120@voicestream.net
Home: (612) 522-0165


Re: design patterns

Posted by Scott Eade <se...@backstagetech.com.au>.
From: <ps...@comcen.com.au>
> I would call my web site page-centric, as the Turbine  2.1
> security model forced me to have a java class for each page, and
> its straight forward to populate the velocity context in the class
> that sits behind each page...

Patrick,

Turbine actually doesn't force you to have a class for each page.
You can have one Default.class for each package that sets the 
security for all of the templates in the corresponding directory. 
Then our friend the pull model can be used to retrieve the rest 
of the data you need.

Cheers,

Scott


Re: design patterns

Posted by Jason van Zyl <jv...@apache.org>.
On 7/30/01 8:18 PM, "psaunder@comcen.com.au" <ps...@comcen.com.au> wrote:

> Hi Dan,
> I know exactly where you are coming from.
> I am sorta new to the web development scene and originally
> bounced from webmacro to freemarker to velocity, and ended
> up using Turbine with Velocity.
> Turbine really does take 80% of the effort out of building a
> web site, not to mention all the extremely useful and rich
> featured services it has available.
> I'm sure there are many others who have similar stories to mine.
> 
> I would call my web site page-centric, as the Turbine  2.1
> security model forced me to have a java class for each page,

I just want to point out that your statement is not correct.
You don't need a java class for each page: for security purposes,
or for access to bean like objects in templates. If you want to
discuss this further post to the turbine list.

> and
> its straight forward to populate the velocity context in the class
> that sits behind each page...
> 
> Regards,
> 
> Patrick.
> 
> Quoting Dan Bachelder <ch...@chowda.net>:
> 
>> I'm rather new to the velocity scene...
>> 
>> I was kind of wondering about how people design to use the velocity in
>> their
>> web apps?
>> 
>> do you use a servlet to sit in front and serve all the pages? do you
>> design
>> modules based on like-content? do you use a page-centric view? or
>> something
>> completely different?
>> 
>> discussion or resources are welcome... thanks.
>> 
>> dan
>> 
>> 

-- 

jvz.

Jason van Zyl

http://tambora.zenplex.org
http://jakarta.apache.org/turbine
http://jakarta.apache.org/velocity
http://jakarta.apache.org/alexandria
http://jakarta.apache.org/commons



Re: design patterns

Posted by ps...@comcen.com.au.
Hi Dan,
I know exactly where you are coming from.
I am sorta new to the web development scene and originally
bounced from webmacro to freemarker to velocity, and ended
up using Turbine with Velocity.
Turbine really does take 80% of the effort out of building a
web site, not to mention all the extremely useful and rich
featured services it has available.
I'm sure there are many others who have similar stories to mine.

I would call my web site page-centric, as the Turbine  2.1
security model forced me to have a java class for each page, and
its straight forward to populate the velocity context in the class
that sits behind each page...

Regards,

Patrick.

Quoting Dan Bachelder <ch...@chowda.net>:

> I'm rather new to the velocity scene...
> 
> I was kind of wondering about how people design to use the velocity in
> their
> web apps?
> 
> do you use a servlet to sit in front and serve all the pages? do you
> design
> modules based on like-content? do you use a page-centric view? or
> something
> completely different?
> 
> discussion or resources are welcome... thanks.
> 
> dan
> 
> 

war

Posted by Dan Bachelder <ch...@chowda.net>.
this is probably just me being an idiot.. but when I use servlet_example 1
in a blown out dir structure everything works great... when I make a WAR out
of that structure, velocity can't find sample.vm.. do I need to make a
change in the source when I want to use a WAR? (i'm using tomcat 4 and
velocity 1.1)


RE: design patterns

Posted by David Rees <dr...@ebetinc.com>.
> -----Original Message-----
> From: Dan Bachelder [mailto:chowda@chowda.net]
>
> I'm rather new to the velocity scene...
>
> I was kind of wondering about how people design to use the
> velocity in their
> web apps?
>
> do you use a servlet to sit in front and serve all the pages? do
> you design
> modules based on like-content? do you use a page-centric view? or
> something
> completely different?
>
> discussion or resources are welcome... thanks.

I've found that mapping *.vm (or whatever extension you want) to a servlet
works pretty well.  Then use request.getServletPath() in handleRequest to
figure out which template to load.

This is extremely convenient when working with web designers, because then
you can add your document root to Velocitiy's path, and keep your .vm and
.gif and .jpg files all under the same directory.  Make sure you have
Apache/Tomcat configured properly, I've found that a sloppy config can
reveal your templates (not a big deal with all the logic in your servlets,
anyway.

-Dave