如何在 ui:repeat 中显示哈希图列表?
我在使用 JSF 在 Facelets 中显示某些数据时遇到问题。我有哈希图列表:
List<Map<String, String>> persons = new LinkedList<Map<String,String>>();
public List getPersons() {
return this.persons;
}
我从数据库中得到如下信息:
while(rs.next()) {
Map<String,String> result = new HashMap<String,String>();
result.put("name", rs.getString(1));
result.put("category", rs.getString(2));
this.persons.add(result);
}
所以,我的问题是如何在 xhtml 中显示每个地图的信息。我尝试使用 ui:repeat 但它是错误的,所以我需要帮助。我必须有姓名和家庭的吸气剂,但我应该如何添加它?
<ui:repeat value="#{class.persons}" var="persons">
<h:outputText value="#{persons['name'}"/>
<h:outputText value="#{persons['family'}"/>
</ui:repeat>
我希望您理解我的问题并帮助我解决它。提前致谢!
I have a problem using JSF to display some data in Facelets. I have list of hashmaps:
List<Map<String, String>> persons = new LinkedList<Map<String,String>>();
public List getPersons() {
return this.persons;
}
I get this as follows from database:
while(rs.next()) {
Map<String,String> result = new HashMap<String,String>();
result.put("name", rs.getString(1));
result.put("category", rs.getString(2));
this.persons.add(result);
}
So, my problem is how to display info for every map in xhtml. I try to used ui:repeat
but it is wrong so I need help. I must have getter for name and family but how should I add it?
<ui:repeat value="#{class.persons}" var="persons">
<h:outputText value="#{persons['name'}"/>
<h:outputText value="#{persons['family'}"/>
</ui:repeat>
I hope you understand my problem and will help me to fix it. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,
#{persons}
是一个Map
。您可以像普通 Javabean 一样通过键访问映射值。因此#{person.name}
将返回map.get("name")
。所以,应该这样做:
(我只将
persons
重命名为person
,因为它本质上只代表一个人)顺便说一下,以下方式也是有效的,如果你有一个包含句点的映射键,这实际上是唯一的方法:
(你看,你很接近,你只是忘记了右大括号)
但是,通常的做法是创建一个 Javabean 类而不是
Map
(如果它实际上代表一个) 实体。并将其作为
List
提供给视图。The
#{persons}
is thus aMap<String, String>
. You can access map values by keys in the same way as normal Javabeans. So#{person.name}
will returnmap.get("name")
.So, this should do:
(I only renamed
persons
toperson
, because it essentially represents only one person)The following way is by the way also valid and it would actually be the only way if you have a map key which contained periods:
(you see, you were close, you only forgot the closing brace)
The normal practice, however, is to create a Javabean class instead of a
Map
if it actually represents an entity.And feed it as
List<Person>
to the view.