tastyminerals
tastyminerals

Reputation: 6538

Why "c:forEach" loop returns nothing in this jsp?

This should have been easy. I created c:forEach loop in my jsp page. This loop should display the objects that were extracted by my Service class from the db. Service class extracts the List correctly and the List in not empty. So the problem should be in my jsp page. When creating this jsp I followed the tutorial and that's why I don't understand what is not working here. Here is it:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link
    href="${pageContext.request.contextPath}/resources/css/poem-list.css"
    rel="stylesheet" />
<title>Poem Collection</title>
</head>
<body>
    <div id="maincontainer">
                <p>Poems</p>
            </div>
        </div>
        <div id="contentcolumn">
            <form method="GET">
                <table align="center">
                    <!-- tbody -->
                    <c:forEach var="poem" items="${poems}">
                        <tr>
                            <td>
                                <p class="title">${poem.title}</p>
                                <p class="annotation">by DUMMY<br> 
                                    written in ${poem.date}<a href="${pageContext.request.contextPath}/collection.html">
                                <input type="button" class="view" value="View" />
                                </a>
                                </p>
                            </td>
                        </tr>
                    </c:forEach>
                    <!-- /tbody-->
                </table>
            </form>
        </div>
        <div id="bottom-footer">
            <p>My Collection</p>
        </div>
    </div>
</body>
</html>

Controller that displays collection and invokes poem-list.jsp with forEach loop.

@RequestMapping(value = "/collection")
public ModelAndView collectionPage() {
    ModelAndView modelAndView = new ModelAndView("poem-list");
    List<Poem> poems = poemService.getPoems();
    modelAndView.addObject("poems", poems);
    return new ModelAndView("poem-list");
}

And my Service class that extracts data from db:

@SuppressWarnings("unchecked")
public List<Poem> getPoems() {
    return getCurrentSession().createQuery("from Poem").list();
}

Upvotes: 0

Views: 369

Answers (2)

Braj
Braj

Reputation: 46841

Most common cause is that poems are not set in any scope.

Please validate it using below code:

<c:if test="${poems ==null || poems.size()==0}">Poems are not found</c:if>

Upvotes: 1

Laabidi Raissi
Laabidi Raissi

Reputation: 3333

In you controller you are instantiating the ModelAndView modelAndView but returning another instance:

return new ModelAndView("poem-list");

replace it with:

return modelAndView;

and it should work

Upvotes: 4

Related Questions