You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Terry Steichen <te...@net-frame.com> on 2002/01/18 14:59:49 UTC

Failure to display results that contain a dot?

Let's say I have some code that issues the SQL query "select field1 from recs1", places the resulting records into a vector 'results' , inserts the vector the context as 'c_results' and then runs a template with the following VTL to display them:

#foreach($result in $c_results)
    $result.field1
#end

This works fine.  

Now I change the SQL query to: "select recs1.field1 from recs1" and try to display the results using the following VTL expression:

#foreach($result in $c_results)
    $result.recs1.field1
#end

This does *not* work( even though it generates the correct number of rows/results) - all that is displayed is the literal "$result.recs1.field1".  

However, if I change the VTL to following:

#foreach($result in $c_results)
    $result
#end

The display now expands each $result to show the correct value, but inside curly brackets - that is, it shows something like this for each result:

{recs1.field1=xxxxx}  [where xxxx is the actual - correct- value]

As a further test, I changed the template to the following:

#foreach($result in $c_results)
    ${result}.recs1.field1
#end

which produced similar entries of the following form:

 {recs1.field1=xxxxx}.recs1,field1

In other words, it appears that if there is a dot ('.') notation in the results vector element (the VTL varb '$result' in this example), the template *cannot* interpret it correctly.  The (quite serious) problem is that this behavior means that you (apparently) can't use the Velocity template to handle the display of results from non-trivial queries (such as "select table1.name, table2.id from table1, table2").

Assuming the above is reasonably clear and that I haven't goofed myself, I suspect this behavior may be related to the fact that a Velocity can invoke methods but will not retrieve properties.  

I wonder if anyone can suggest where I goofed or, if not, a work around?

Regards,

Terry


Re: Failure to display results that contain a dot?

Posted by Terry Steichen <te...@net-frame.com>.
Addendum:

I discovered that Velocity works fine if the SQL statement is constructed so
it does *not* contain a dot.  For example, the following VTL properly
displays the results of the query "select field1, field2 from table1,
table2" works fine (provided, of course, that 'field1 and field2 are unique
field names):

#foreach($result in $c_results)
    $result.field1 - $result.field2
#end

So I have a workaround (whew!).

Regards,

Terry

----- Original Message -----
From: "Terry Steichen" <te...@net-frame.com>
To: <ve...@jakarta.apache.org>
Sent: Friday, January 18, 2002 8:59 AM
Subject: Failure to display results that contain a dot?


Let's say I have some code that issues the SQL query "select field1 from
recs1", places the resulting records into a vector 'results' , inserts the
vector the context as 'c_results' and then runs a template with the
following VTL to display them:

#foreach($result in $c_results)
    $result.field1
#end

This works fine.

Now I change the SQL query to: "select recs1.field1 from recs1" and try to
display the results using the following VTL expression:

#foreach($result in $c_results)
    $result.recs1.field1
#end

This does *not* work( even though it generates the correct number of
rows/results) - all that is displayed is the literal "$result.recs1.field1".

However, if I change the VTL to following:

#foreach($result in $c_results)
    $result
#end

The display now expands each $result to show the correct value, but inside
curly brackets - that is, it shows something like this for each result:

{recs1.field1=xxxxx}  [where xxxx is the actual - correct- value]

As a further test, I changed the template to the following:

#foreach($result in $c_results)
    ${result}.recs1.field1
#end

which produced similar entries of the following form:

 {recs1.field1=xxxxx}.recs1,field1

In other words, it appears that if there is a dot ('.') notation in the
results vector element (the VTL varb '$result' in this example), the
template *cannot* interpret it correctly.  The (quite serious) problem is
that this behavior means that you (apparently) can't use the Velocity
template to handle the display of results from non-trivial queries (such as
"select table1.name, table2.id from table1, table2").

Assuming the above is reasonably clear and that I haven't goofed myself, I
suspect this behavior may be related to the fact that a Velocity can invoke
methods but will not retrieve properties.

I wonder if anyone can suggest where I goofed or, if not, a work around?

Regards,

Terry





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


Re: Failure to display results that contain a dot?

Posted by Terry Steichen <te...@net-frame.com>.
> What is the object $result?
> Does it have a get method?  Behind the scenes, velocity is either doing
>   result.get("field1")
> Or
>   result.getField1()
>
> I suspect the former.
>
> Hence, you are mixing two notations, SQL and VTL, and expecting Velocity
to
> somehow 'know' that you are expressing things in SQL.  The solution is to
> do, if my hunch about the get( String ) is correct :
>
> #foreach( $result in $c_results )
>   $result.get("recs1.field1")
> #end
>
> Does that work?

Yup.  Many thanks.  (BTW, the individual 'result' object is a HashMap so, in
hindsight, your suggestion should have been an obvious candidate for me to
try.)  Now I can *really* get to work!  (Which is to say, no more excuses.)

Regards,

Terry

PS: Similar thanks to Denis for the same advice.




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


Re: Failure to display results that contain a dot?

Posted by "Geir Magnusson Jr." <ge...@yahoo.com>.
On 1/18/02 8:59 AM, "Terry Steichen" <te...@net-frame.com> wrote:

> Let's say I have some code that issues the SQL query "select field1 from
> recs1", places the resulting records into a vector 'results' , inserts the
> vector the context as 'c_results' and then runs a template with the following
> VTL to display them:
> 
> #foreach($result in $c_results)
>   $result.field1
> #end
> 
> This works fine. 
> 
> Now I change the SQL query to: "select recs1.field1 from recs1" and try to
> display the results using the following VTL expression:
> 
> #foreach($result in $c_results)
>   $result.recs1.field1
> #end
> 
> This does *not* work( even though it generates the correct number of
> rows/results) - all that is displayed is the literal "$result.recs1.field1".
> 


What is the object $result?

Does it have a get method?  Behind the scenes, velocity is either doing

  result.get("field1")

Or

  result.getField1()

I suspect the former.

Hence, you are mixing two notations, SQL and VTL, and expecting Velocity to
somehow 'know' that you are expressing things in SQL.  The solution is to
do, if my hunch about the get( String ) is correct :

#foreach( $result in $c_results )
  $result.get("recs1.field1")
#end

Does that work?

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


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


Re: Failure to display results that contain a dot?

Posted by Denis <ji...@respublica.fr>.
Hi Terry

> In other words, it appears that if there is a dot ('.') notation 
> in the results vector element (the VTL varb '$result' in this 
> example), the template *cannot* interpret it correctly.  The 
> (quite serious) problem is that this behavior means that you 
> (apparently) can't use the Velocity template to handle the display 
> of results from non-trivial queries (such as "select table1.name, 
> table2.id from table1, table2").
>
> Assuming the above is reasonably clear and that I haven't goofed 
> myself, I suspect this behavior may be related to the fact that a 
> Velocity can invoke methods but will not retrieve properties.

What is a property in Java? You did not specify what was the type 
of the objects you put in your Vector. Velocity can render 
$result.recs1 if the result object has a method getRecs1(), 
recs1(), or a method get(String) that returns an object.

-- Denis.

>
> I wonder if anyone can suggest where I goofed or, if not, a work 
> around?
>
> Regards,
>
> Terry
>


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