As Paul mentioned, printing the class name helps tremendously.  The $response 
(SolrQueryResponse) can contain all sorts of nested objects of various types, 
so it's necessary to know what kind of object it is (generally, but not alway) 
to be able to navigate it effectively.

A shortcut, you can actually use $sentence.class

Velocity leverages naming conventions and treats things like "beans" even if 
they technically aren't, so if it has a getFoo() method, $object.foo will work 
to retrieve the value.  

I'm curious what kind of object you have for $sentence.  Often you'll get a 
NamedList, which has a get(Object) method.  Likewise with a HashMap.   

You may be able to get away with using $sentence.index and $sentence.text.  
Knowing the object type will point you to everything you can call on it.  And 
Velocity's documentation is here: 
<https://velocity.apache.org/engine/releases/velocity-1.7/vtl-reference-guide.html>
 (geeky and maybe slightly tricky at first, but fairly straightforward once you 
know what object types you're dealing with)

        Erik


On Oct 9, 2012, at 02:33 , Paul Libbrecht wrote:

> Lance,
> 
> this is the kind of fun that happens with Velocity all day long...
> 
> In general, when it outputs the variable name, it's the that the variable is 
> null; this can happen when a method is missing for example.... 
> There are actually effective uses of this 
> brain-dead-debugger-oriented-practice!
> 
> I would suppose that the class of your $sentence is not something that has a 
> get(String) method.
> With a normal debugging, this should be shown in a console.
> This is strengthened by the fact that your output of $sentence is not exactly 
> the same as a the output of  java.util.HashMap for example.
> 
> When in this situation, I generally make
>   Raw data: $sentence of class $sentence.getClass()
> (note: class is not a bean property, you need the method call)
> 
> Hope it helps.
> 
> Paul
> 
> PS: to stop this hell, I have a JSP pendant to the VelocityResponseWriter, is 
> this something of interest for someone so that I contribute it?
> 
> 
> 
> Le 9 oct. 2012 à 04:39, Lance Norskog a écrit :
> 
>> I am adding something to Solaritas, under /browse. One bit of Velocity
>> code does not unpack the result structure the way I think it should.
>> Please look at this- there is something I am missing about
>> tree-walking.
>> 
>> Here is the XML from a search result:
>> <lst name="outer">
>> <lst name="sentence">
>>   <int name="index">0</int>
>>   <str name="text">
>>   A bunch of words
>>   </str>
>> </lst>
>> more sentences ....
>> </lst>
>> 
>> Here is my Velocity code:
>> #foreach($sentence in $outer)
>>   Raw data: $sentence
>>   <br/>
>>   #set($index = $sentence.get('index'))
>>   #set($text = $sentence.get('text'))
>>   <div class="text">
>>     Index: $index
>>     <br/>
>>     Text: $text
>>     <br/>
>>   </div>
>> #end
>> 
>> Here is the output:
>> 
>>   Raw data: sentence={index=0,text= A bunch of words}
>>   Index: $index
>>   Text: $text
> 

Reply via email to