how do I iterate though a java list in stringtemplate? -


i want iterate through hibernate query results inside stringtemplate. i've been looking examples can't find anything.

can please help? thanks

the syntax looks like

<items :{ item | <item> }> 

putting in java:

list<string> teams = arrays.aslist("cats", "birds", "turtles"); st s = new st( "<teams :{team | <team> }>"); s.add("teams", teams); system.out.println(s.render()); 

in example iterate on list , print each. result printed is:

cats birds turtles  

we can explore syntax makes happen. before do, remember, default delimiters in stringtemplate less < , greater >. since didn't specify different delimiters use in our example.see more delimiters

:{ } 

this set of symbols, colon : , open , closed brace {} can read "for each". in example template, code reads, each team in teams print team. left side of vertical pipe | indicates variable created each iteration. hold current team list of teams. print composed of <team> on right side of vertical pipe | , left side of closing brace }. on right side of vertical pipe | , before closing base } evaluated printed.

:{ current value | in here printed } 

in order build on concept, let's use more complex data structure.

public class player {     private string name;     private int age;      public person(string name, int age) {          this.name = name;          this.age = age;      }     public int getage() { return age; }     public string getname() { return name; } } 

now can create few players our team:

player[] players = new player[] {     new player("bill", 29),     new player("steve", 30),     new player("toby", 15) };  string playertemplate = "<players:{ player |<player.name> <player.age> <\\n>}>" st s = new st( playertemplate  ); s.add("players", arrays.aslist(players)); system.out.println(s.render()); 

giving result of

bill 29 steve 30 toby 15 

couple of things note. didn't access properties age , name directly. st called methods getage , getname. st doesn't properties. instead looks find access methods.

what if wanted iterate on list contained list. can well. first, let's build our data structure , fill couple of lists.

list<list<string>> listoflists = aslist(     aslist("one", "two", "three"),      aslist("four", "five"),      aslist("six", "seven", "eight", "nine") ); 

the template following.

<list :{ items |<items :{ item |<item> }><\n>}> 

our template in case combination. outer shell iterate on list hand in.

 <list :{ items |  print   }> 

then each item print out items in list.

<items :{ item |<item> }> 

once put together

string template = "<list :{ items |<items :{ item |<item> }><\\n>}>"; st st = new st( template); st.add("list", listoflists); system.out.println(st.render()); 

we result looks following.

one 2 3  4 5  6 7 8 9  

building on concept little more can create second data structure contains list of players. demonstrate how iterate within iteration.

first thing need data structure contains list. can create team our players part.

public class team {     private list<player> players;     private string name;      public team (string name, list<player> players) {         this.players = players;         this.name = name;     }      public list<player> getplayers() {         return players;     }      public string getname() {         return name;     } } 

notice our team contains players. composition allow build 2 iterations.

now have our data structure lets set make couple of teams players.

list<team> teams = aslist(         new team("billings", aslist(                 new player("bill", 29),                 new player("steve", 30),                 new player("toby", 15)         )),         new team("laurel", aslist(                 new player("chad", 32),                 new player("chuck", 29),                 new player("will", 24),                 new player("ben", 26)         )) ); 

now lets create template , fill in few details:

string simpleteamtemplate = "<teams:{ team |<team.name> has <length(team.players)> players<\\n>}>";  st template = new st( simpleteamtemplate ); template.add("teams", teams);  system.out.println(template.render()); 

that print out

billings has 3 players laurel has 4 players 

our simple template same our first template above. real difference using build in method length(). see more on functions here

let's increase complexity of templates little add in our second iteration.

first create our playerstemplate. identical our playertemplate template above. difference have our players coming team: team.players.

string playerstemplate = "<team.players :{ player |<player.name> <player.age><\\n>}>"; 

now construct second template contains first. in template can iterate on teams , each team print out name, number of players length(team.players), , in playerstemplate.

string teamtemplate = "<teams:{ team |<team.name> has <length(team.players)> players<\\n>"+playerstemplate+"}>";  

now let's put together.

st teamstemplate = new st( simpleteamtemplate); teamstemplate.add("teams", teams);  system.out.println(teamstemplate.render()); 

that print following.

billings has 3 players bill 29 steve 30 toby 15 laurel has 4 players chad 32 chuck 29 24 ben 26 

now, aren't going want combine templates in way. appending strings compose templates rather silly. stringtemplate offers tools make combination of partial templates easy. if curious combining templates can find out more here


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -