user1649415
user1649415

Reputation: 1095

Printing an array/collection of javabeans in JSP

Is there a standard way of printing an array/collection elements of javabeans in JSP? All I know is the <jsp:getProperty> tag which can't do this. I know it can be done using custom tags, but it being such an essential requirement should be provided by JSP.

Also, I have read that using setAttribute() method of PageContext, ServletContext etc we can in a Servlet get the bean and work on it, but it's giving me null value.

pageContext.getAttribute("beanPropertyVariable")  //set in page scope
application.getAttribute("beanPropertyVariable")  //set in application scope  

How can I achieve this?

Upvotes: 0

Views: 1398

Answers (2)

PermGenError
PermGenError

Reputation: 46408

you can use JSTL c:foreach tag like below

 <c:forEach items="${list}" var="var">
     ${var}<br/>
 </c:forEach>

Upvotes: 0

BalusC
BalusC

Reputation: 1108642

The standard way is using JSTL <c:forEach>.

Assuming that ${beans} represents the collection of javabeans, here's an example:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:forEach items="${beans}" var="bean">
    ${bean.property1}<br/>
    ${bean.property2}<br/>
    ${bean.property3}<br/>
</c:forEach>

That's also the simplest way you can get.

See also:

Upvotes: 6

Related Questions