如何在stringtemplate中迭代java列表?
我想在stringtemplate中遍历hibernate查询结果。我一直在找例子,但什么也找不到 你能帮忙吗?谢谢如何在stringtemplate中迭代java列表?,java,stringtemplate-4,Java,Stringtemplate 4,我想在stringtemplate中遍历hibernate查询结果。我一直在找例子,但什么也找不到 你能帮忙吗?谢谢%staffForOrg:{员工| %staffForOrg: {staff| <tr> <td><a href="#%staff.id%a" class="directory " id="%staff.id%1" onclick="javascript: window.location='StaffInfo.html?id=%
%staffForOrg:{员工|
%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>
}%
}%
这段代码工作得很好 staffForOrg是我的模型列表。我使用hibernate检索记录。语法如下所示
<items :{ item | <item> }>
我们可以探索实现这一点的语法。在此之前,请记住,StringTemplate中的默认分隔符小于
。因为我们没有指定不同的分隔符,所以在我们的示例中将使用
这组符号,冒号:
和左大括号{}
可以理解为“for each”。在示例模板中,代码为teams
printteams
中的每个teams
。垂直管道的左侧表示将为每次迭代创建的变量。它将保存团队列表中的当前团队。打印内容由垂直管道右侧的
和闭合支架左侧的
组成。任何位于垂直管道右侧且在闭合底座之前的内容都将被计算为打印
:{ current value | everything in here will be printed }
为了构建这个概念,让我们使用更复杂的数据结构
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; }
}
现在我们可以为我们的团队创造一些球员:
Player[] players = new Player[] {
new Player("Bill", 29),
new Player("Steve", 30),
new Player("Toby", 15)
};
String playerTemplate = "<players:{ player |<player.name> is <player.age> <\\n>}>"
ST s = new ST( playerTemplate );
s.add("players", Arrays.asList(players));
System.out.println(s.render());
有几件事需要注意。我们没有直接访问属性年龄和名称。ST调用了getAge和getName方法。ST不看房产。相反,它希望找到访问方法
如果我们只是想迭代一个包含另一个列表的列表呢。我们也可以这样做。
首先,让我们构建数据结构,并用几个列表填充它
List<List<String>> listOfLists = asList(
asList("One", "Two", "Three"),
asList("Four", "Five"),
asList("Six", "Seven", "Eight", "Nine")
);
在这个概念的基础上,我们可以创建第二个包含玩家列表的数据结构。这将演示如何在迭代中迭代
我们首先需要的是一个包含列表的数据结构。为此,我们可以创建一个团队,让我们的球员成为其中的一部分
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;
}
}
我们的简单模板与上面的第一个模板大致相同。唯一真正的区别是我们使用的是STlength()
提供的内置方法
让我们稍微增加一下模板的复杂性,以便在第二次迭代中添加
首先,我们将创建我们的playersTemplate
。这与上面的playerTemplate
模板几乎相同。唯一的区别是我们的球员来自团队
:团队。球员
String playersTemplate = "<team.players :{ player |<player.name> is <player.age><\\n>}>";
现在让我们把这些放在一起
ST teamsTemplate = new ST( simpleTeamTemplate);
teamsTemplate.add("teams", teams);
System.out.println(teamsTemplate.render());
这将为我们打印以下内容
<list :{ items |<items :{ item |<item> }><\n>}>
One Two Three
Four Five
Six Seven Eight Nine
Billings has 3 players
Bill is 29
Steve is 30
Toby is 15
Laurel has 4 players
Chad is 32
Chuck is 29
Will is 24
Ben is 26
现在,您不会真的希望以这种方式组合模板。将字符串附加在一起组成模板是相当愚蠢的。StringTemplate提供了使部分模板的组合非常容易的工具。如果您对组合模板感兴趣,您可以找到更多信息如果您可以迭代java集合,那么您可以将结果列表发送到那里并进行操作。这对于任何模板引擎来说都应该是非常标准的…如果Cats(例如)是一个Cats类而不是字符串,会发生什么呢。我想说的是,如果一个列表中可能有一个列表呢。我该如何解决这个问题。我在解析时遇到了一些问题,比如“没有这样的元素异常”。我不知道我能否在评论中完整地回答你的问题。不过我会试试的。如果您正在迭代自己的类,那么可以调用getter。在这个示例中,让我们假设每个团队都是一个具有getName方法的团队类。我们可以调用将调用该方法的。“”接口组{public String getName();}@AnirbanDebnath@AnirbanDebnath我已经更新了示例以回答您的问题。我希望这有帮助。
<items :{ item |<item> }>
String template = "<list :{ items |<items :{ item |<item> }><\\n>}>";
ST st = new ST( template);
st.add("list", listOfLists);
System.out.println(st.render());
One Two Three
Four Five
Six Seven Eight Nine
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;
}
}
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)
))
);
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());
Billings has 3 players
Laurel has 4 players
String playersTemplate = "<team.players :{ player |<player.name> is <player.age><\\n>}>";
String teamTemplate = "<teams:{ team |<team.name> has <length(team.players)> players<\\n>"+playersTemplate+"}>";
ST teamsTemplate = new ST( simpleTeamTemplate);
teamsTemplate.add("teams", teams);
System.out.println(teamsTemplate.render());
Billings has 3 players
Bill is 29
Steve is 30
Toby is 15
Laurel has 4 players
Chad is 32
Chuck is 29
Will is 24
Ben is 26