You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Tim Colson <tc...@cisco.com> on 2002/07/24 18:40:16 UTC

RE: how do i deal with [preparing db results for VTL]?

The other responses which suggest creating a simple bean with
getter/setters works nicely. However, it's also simple to create an
array of hashmaps.

Pseudo Code:
// Create a list to hold each row of data
ArrayList userlist = new ArrayList();
stmt = "select userid, username from usertable"
 while (stmt has results)
  // create an anonymous hash to store the row
  Hashmap hm = new HashMap(); 
  hm.put("id", results.get("userid"));
  hm.put("name", results.get("username"));
  // add this 'row' of data onto the list
  userlist.add(hm);
 end
// Put the userlist into the Velocity 'context'
ctx.put("userlist",userlist);

--
Then, in the template, it's as easy as:

#foreach ($user in $userlist) 
 $user.id - $user.name
#end

Your mileage will vary of course 

Cheers,
Tim


> > userid ,username
> > _____________
> > 1,"james"
> > 2,"michael"
> > ...
> > ...
> > and i want to build the Velocity variables to hold the results
> > how do i construct it in my java program? like that?
> > 
> > Vector v1 = new Vector();
> > v1.addElement(1);
> > v1.addElement(2);
> > ...
> > 
> > Vector v2 = new Vector();
> > v2.addElement("james");
> > v2.addElement("michael");
> > ...
> > 
> > context.put("v1",v1);
> > context.put("v2",v2);
> > 
> > or construct two arraies?
> > _______________
> > 
> > array1 = {1,2,...};
> > array2={"james","michael",...};
>


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


Re: how do i deal with [preparing db results for VTL]?

Posted by Claude Brisson <cl...@savoirweb.com>.
Geir wrote :

> You also can make little utils that use a ResultSet's meta-data to
> automatically generate one of these list/map representations.

that's exactly what Velosurf does for you, you may try it if you've got several mappings of this kind in your application.

CloD

----- Original Message ----- 
From: "Geir Magnusson Jr." <ge...@adeptra.com>
To: <ve...@jakarta.apache.org>
Sent: jeudi 25 juillet 2002 04:24
Subject: Re: how do i deal with [preparing db results for VTL]?


> On 7/24/02 12:40 PM, "Tim Colson" <tc...@cisco.com> wrote:
> 
> > The other responses which suggest creating a simple bean with
> > getter/setters works nicely. However, it's also simple to create an
> > array of hashmaps.
> >
> 
> [SNIP]
> 
> > 
> > --
> > Then, in the template, it's as easy as:
> > 
> > #foreach ($user in $userlist)
> > $user.id - $user.name
> > #end
> > 
> > Your mileage will vary of course
> 
> I was going to suggest the same thing.  Is pretty easy, and fast for those
> of us too lazy to write a bean class for the data :)
> 
> You also can make little utils that use a ResultSet's meta-data to
> automatically generate one of these list/map representations.
> 
> -- 
> 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>
> 
> 


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


Re: how do i deal with [preparing db results for VTL]?

Posted by "Geir Magnusson Jr." <ge...@adeptra.com>.
On 7/24/02 12:40 PM, "Tim Colson" <tc...@cisco.com> wrote:

> The other responses which suggest creating a simple bean with
> getter/setters works nicely. However, it's also simple to create an
> array of hashmaps.
>

[SNIP]

> 
> --
> Then, in the template, it's as easy as:
> 
> #foreach ($user in $userlist)
> $user.id - $user.name
> #end
> 
> Your mileage will vary of course

I was going to suggest the same thing.  Is pretty easy, and fast for those
of us too lazy to write a bean class for the data :)

You also can make little utils that use a ResultSet's meta-data to
automatically generate one of these list/map representations.

-- 
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>