I want to iterate through a hibernate query results inside stringtemplate. I've been looking for examples but I can't find anything.
can you please help? thanks
I want to iterate through a hibernate query results inside stringtemplate. I've been looking for examples but I can't find anything.
can you please help? thanks
%staffForOrg: {staff|
<tr>
<td><a href="#%staff.id%a" class="directory " id="%staff.id%1" onclick="javascript: window.location='StaffInfo.html?id=%staff.id%';">%staff.telephoneNumber%</a></td>
</tr>
}%
this code works perfectly.
staffForOrg is a list of my model. I used hibernate to retrieve the records.
The syntax looks like
Putting it together in Java:
In this example, I iterate over the List and print each team that is in the
teams
list. The result that would be printed is:We can explore the syntax that makes this happen. Before we do, remember, that the default delimiters in StringTemplate are less than
<
and greater than>
. Since we didn't specify a different delimiter<
>
will be what we use in our example.See more about delimitersThis set of symbols, the colon
:
and the open and closed brace{}
can be read as "for each". In the example template, the code reads, for eachteam
inteams
printteam
. The left side of the vertical pipe|
indicates the variable that will be created for each iteration. It will hold the current team from the list of teams. The print is composed of the<team>
on the right side of the vertical pipe|
and the left side of the closing brace}
. Anything that is on the right side of the vertical pipe|
and before the closing base}
will be evaluated to be printed.In order to build on the concept, let's use a more complex data structure.
Now we can create a few players for our team:
Giving a result of
Couple of things to note. We didn't access the properties age and name directly. ST called the methods getAge and getName. ST doesn't look to the properties. Instead, it looks to find the access methods.
What if we just wanted to iterate over a list that contained another list. We can do that as well. First, let's build up our data structure and fill it with a couple of lists.
The template will look like the following.
Our template, in this case, will just be a combination. The outer shell will iterate over the list we will hand in.
Then for each item, we will print out the items in its list.
Once we put it all together
We get a result that looks like the following.
Building on this concept a little more we can create a second data structure that contains a list of players. This will demonstrate how to iterate within iteration.
The first thing we will need is a data structure that contains a list. For this we can create a Team for our players to be a part.
Notice that our team contains players. This composition will allow us to build up two iterations.
Now that we have our data structure lets set everything together to make a couple of teams with some players.
Now lets create a template and fill in a few details:
That will print out
Our simple template is just about the same as our first template from above. The only real difference is that we are using a built-in method provided by ST
length()
. See more on functions hereLet's increase the complexity of the templates a little to add in our second iteration.
First, we will create our
playersTemplate
. This is almost identical to ourplayerTemplate
template from above. The only difference is that we have our players coming from ateam
:team.players
.Now we will construct a second template that contains the first. In this template we can iterate over teams and for each team we will print out the
name
, number of playerslength(team.players)
, and everything in theplayersTemplate
.Now let's put that all together.
That will print for us the following.
Now, you aren't really going to want to combine your templates in this way. Appending strings together to compose templates is rather silly. StringTemplate offers tools to make this combination of partial templates very easy. If you are curious about combining templates you can find out more here