如何在JSTL中显示包含objetcs的列表中的数据? [英] how to display data in JSTL for list containing objetcs?
本文介绍了如何在JSTL中显示包含objetcs的列表中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在请求属性中,我有对象列表(例如用户对象),那么如何循环遍历它在jsp页面上显示数据?我可以使用<c:foreach>
,但是我怎么能说它是用户对象和访问属性呢?
In request attribute I have list of objects (say user objects) so how can I loop through it display data on my jsp page? Can I use <c:foreach>
but then how I can say that it is User object and access properties of that?
推荐答案
JSTL/EL不关心确切的类型.您需要确保的是,所讨论的对象具有给定属性的getter方法,以便您可以仅指定属性名称.
JSTL/EL doesn't care about the exact type. All you need to ensure is that the object in question has a getter method for the given property so that you can just specify the property name.
想象一下,
public class User {
private Long id;
private String name;
private Integer age;
// Getters/setters.
}
然后您可以像下面这样循环遍历List<User>
:
then you can loop over a List<User>
like follows:
<table>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td><c:out value="${user.name}" /></td>
<td>${user.age}</td>
</tr>
</c:forEach>
</table>
就是这样.
- EL tag wiki page
- Places where JavaBeans are used?
这篇关于如何在JSTL中显示包含objetcs的列表中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文