how to display data in JSTL for list containing objetcs?

陌路散爱 提交于 2019-12-11 07:12:01

问题


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?


回答1:


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.

Imagine,

public class User {

    private Long id;
    private String name;
    private Integer age;

    // Getters/setters.
}

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>

That's it.

See also:

  • EL tag wiki page
  • Places where JavaBeans are used?


来源:https://stackoverflow.com/questions/7120068/how-to-display-data-in-jstl-for-list-containing-objetcs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!