You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "G.L. Grobe" <ga...@grobe.net> on 2002/04/13 02:48:07 UTC

getting data from vector objs

My getToolSets() method returns a Vector of objects w/ fields of name, desc,
and id's. How do I get each one of these into the <td>'s below? The method
is in a webwork action that queries a database and returns a vector of these
objects.

// the references below are not correct, but give an idea what I'm trying to
do.
#foreach ($set in $toolSets)
<tr>
   <td><input type="checkbox" name="$set.name" value="$set.toolId"></td>
   <td>$set.name</td>
   <td>$set.desc</td>
</tr>
#end

Any help much appreciated.


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


Re: getting data from vector objs

Posted by "G.L. Grobe" <ga...@grobe.net>.
> > Mostly, yes, thanks ... except for the '$yourVector'. That and I got a
bit
> > puzzled because your getName() just returns a String.
> >
> > #foreach($item in $yourVector)
> >
>
> Well, I had no clue what you were doing, so I guessed :)
>
> > So if it is to be a vector that getName returns, then velocity will
iterate
> > through a vector from getName() w/ values of "joe", "frank", "bob", etc
...
> > and the <td>$item.name</td> will pick that up?
> >
> > In otherwords (better if I show you as I understand it) ...
> > ---
> > public class MySets {
> > ...
> >  public Vector getName() { return nameVector; }
> >  public void setNames(Vector ) {
> >     // pull only names here and put in nameVector  ...
> >  };
> >
> >  public Vector getAddr() { return addrVector; }
> >  public void setAddr(Vector) {
> >     // pull only addr's here and put in addrVector  ...
> >  };
> >
> >  public void getVectorData() {
> >     Vector v = dbase.query();
> >     setNames(v);
> >     setAddr(v);
> >  }
> >
> > Then vel template would look like ...
> > #foreach($item in $vectorData)
> >  <td>$item.name</td><td>$item.address</td>
> > #end
> >
> > And of course $item.name here will look for the pattern getName(),
etc...
>
> So the big question is...
>
> What objects are in the Vector returned by dbase.query() ?

The objects returned by dbase.query is a Vector of class objects with fields
as you listed  ...

public class Data implements Serializable {
   String name = "joe";
   String addr = me@place;

   public String getName() {...};
   public String getAddr() {...};
}

After writing this, I think I see what you were referring to w/ your example
now. It was the methods from the template that I was referring to that would
call the accessors methods from the action servlet that return Vectors (not
Strings) afaik.

So if we're on the same page that my webwork action is returning a Vector of
these as my example above shows, my only question is what is the $vectorData
referring to ... or in the case of your example ... $yourVector. Or do I
understand it correctly in the way it's written in the above example.

Thanks.



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


Re: getting data from vector objs

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 4/17/02 10:10 PM, "G.L. Grobe" <ga...@grobe.net> wrote:

> Mostly, yes, thanks ... except for the '$yourVector'. That and I got a bit
> puzzled because your getName() just returns a String.
> 
> #foreach($item in $yourVector)
> 

Well, I had no clue what you were doing, so I guessed :)

> So if it is to be a vector that getName returns, then velocity will iterate
> through a vector from getName() w/ values of "joe", "frank", "bob", etc ...
> and the <td>$item.name</td> will pick that up?
> 
> In otherwords (better if I show you as I understand it) ...
> ---
> public class MySets {
> ...
>  public Vector getName() { return nameVector; }
>  public void setNames(Vector ) {
>     // pull only names here and put in nameVector  ...
>  };
> 
>  public Vector getAddr() { return addrVector; }
>  public void setAddr(Vector) {
>     // pull only addr's here and put in addrVector  ...
>  };
> 
>  public void getVectorData() {
>     Vector v = dbase.query();
>     setNames(v);
>     setAddr(v);
>  }
> 
> Then vel template would look like ...
> #foreach($item in $vectorData)
>  <td>$item.name</td><td>$item.address</td>
> #end
> 
> And of course $item.name here will look for the pattern getName(), etc...

So the big question is...

What objects are in the Vector returned by dbase.query() ?


-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"The greatest pleasure in life is doing what people say you cannot do."
        - Walter Bagehot



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


Re: getting data from vector objs

Posted by "G.L. Grobe" <ga...@grobe.net>.
Mostly, yes, thanks ... except for the '$yourVector'. That and I got a bit
puzzled because your getName() just returns a String.

#foreach($item in $yourVector)

So if it is to be a vector that getName returns, then velocity will iterate
through a vector from getName() w/ values of "joe", "frank", "bob", etc ...
and the <td>$item.name</td> will pick that up?

In otherwords (better if I show you as I understand it) ...
---
public class MySets {
...
   public Vector getName() { return nameVector; }
   public void setNames(Vector ) {
      // pull only names here and put in nameVector  ...
   };

   public Vector getAddr() { return addrVector; }
   public void setAddr(Vector) {
      // pull only addr's here and put in addrVector  ...
   };

   public void getVectorData() {
      Vector v = dbase.query();
      setNames(v);
      setAddr(v);
   }

Then vel template would look like ...
#foreach($item in $vectorData)
   <td>$item.name</td><td>$item.address</td>
#end

And of course $item.name here will look for the pattern getName(), etc...

> If I understand you correctly (and these days, that doesn't seem to be the
> norm for me...) then the objects in your vector have a bunch of fields,
like
>
>    public String name;
>
>  or some such.
>
> So the pattern for accessors would be something like :
>
>   public class VectorThing
>   {
>       String name;
>       String address;
>
>       public String getName()
>       {
>          return name;
>       }
>
>       public String getAddress()
>       {
>          return address;
>       }
>    }
>
> (see the pattern?)
>
> So in the template, you could
>
>   #foreach($item in $yourVector)
>      <td>$item.name</td><td>$item.address</td>
>   #end
>
> And velocity will find the getName() and getAddress() methods and invoke
> them.
>
> The pattern for
>
>   $item.token
>
> Is that vel looks for
>
>   public Object getToken()
>
>   public Object gettoken()
>
>    public Object get(String);
>
> The latter is called with 'token' as the string.  Note that the return
> Object doesn't matter.  I just put it there for readability.



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


Re: getting data from vector objs

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 4/17/02 9:30 PM, "G.L. Grobe" <ga...@grobe.net> wrote:

>>> My getToolSets() method returns a Vector of objects w/ fields of name,
> desc,
>>> and id's. How do I get each one of these into the <td>'s below? The
> method
>>> is in a webwork action that queries a database and returns a vector of
> these
>>> objects.
>>> 
>>> #foreach ($set in $toolSets)
>>> <tr>
>>>  <td><input type="checkbox" name="$set.name" value="$set.toolId"></td>
>>>  <td>$set.name</td>
>>>  <td>$set.desc</td>
>>> </tr>
>>> #end
> 
>> The right way currently is that Velocity won't introspect for fields, only
>> methods.
>> 
>> There are two ways around this.  First, there is an included tool called
> the
>> 'FieldMethodizer' that does what you want for *static* fields.  That can
> of
>> course be extended to do non-static fields.  The second way is to either
> add
>> bean-like accessors, or a generalized get(String) method.
> 
> Not sure I understand how the bean-like accessors should work, or the get()
> method. Are you saying within the getToolSets() method I should break up
> each data field into separate vectors and then have individual getXXX
> methods for each vector so that in my <td> tags I call each of these methods
> there? If I did that, I don't understand how they would iterate correctly.

If I understand you correctly (and these days, that doesn't seem to be the
norm for me...) then the objects in your vector have a bunch of fields, like

   public String name;

 or some such.

So the pattern for accessors would be something like :

  public class VectorThing
  {
      String name;
      String address;

      public String getName()
      { 
         return name;
      }

      public String getAddress()
      {
         return address;
      }
   }

(see the pattern?)

So in the template, you could

  #foreach($item in $yourVector)
     <td>$item.name</td><td>$item.address</td>
  #end

And velocity will find the getName() and getAddress() methods and invoke
them.

The pattern for

  $item.token

Is that vel looks for

  public Object getToken()
 
  public Object gettoken()

   public Object get(String);

The latter is called with 'token' as the string.  Note that the return
Object doesn't matter.  I just put it there for readability.


Does that clear it up?

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Be a giant.  Take giant steps.  Do giant things...


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


Re: getting data from vector objs

Posted by "G.L. Grobe" <ga...@grobe.net>.
> > My getToolSets() method returns a Vector of objects w/ fields of name,
desc,
> > and id's. How do I get each one of these into the <td>'s below? The
method
> > is in a webwork action that queries a database and returns a vector of
these
> > objects.
> >
> > #foreach ($set in $toolSets)
> > <tr>
> >  <td><input type="checkbox" name="$set.name" value="$set.toolId"></td>
> >  <td>$set.name</td>
> >  <td>$set.desc</td>
> > </tr>
> > #end

> The right way currently is that Velocity won't introspect for fields, only
> methods.
>
> There are two ways around this.  First, there is an included tool called
the
> 'FieldMethodizer' that does what you want for *static* fields.  That can
of
> course be extended to do non-static fields.  The second way is to either
add
> bean-like accessors, or a generalized get(String) method.

Not sure I understand how the bean-like accessors should work, or the get()
method. Are you saying within the getToolSets() method I should break up
each data field into separate vectors and then have individual getXXX
methods for each vector so that in my <td> tags I call each of these methods
there? If I did that, I don't understand how they would iterate correctly.



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


Re: getting data from vector objs

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 4/12/02 8:48 PM, "G.L. Grobe" <ga...@grobe.net> wrote:

> My getToolSets() method returns a Vector of objects w/ fields of name, desc,
> and id's. How do I get each one of these into the <td>'s below? The method
> is in a webwork action that queries a database and returns a vector of these
> objects.
> 
> // the references below are not correct, but give an idea what I'm trying to
> do.
> #foreach ($set in $toolSets)
> <tr>
>  <td><input type="checkbox" name="$set.name" value="$set.toolId"></td>
>  <td>$set.name</td>
>  <td>$set.desc</td>
> </tr>
> #end
> 
> Any help much appreciated.
> 

The right way currently is that Velocity won't introspect for fields, only
methods.

There are two ways around this.  First, there is an included tool called the
'FieldMethodizer' that does what you want for *static* fields.  That can of
course be extended to do non-static fields.  The second way is to either add
bean-like accessors, or a generalized get(String) method.

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting

Age and treachery will always triumph over youth and talent


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