-1

So I have a simple JSP page to display some information like this:

<%@ page import="db.DataManager"%>
<%@ page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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">
<title></title>
</head>
<%
    DataManager dm = (DataManager) application.getAttribute("datamanager");
    ArrayList<DataManager.Author> authors = dm.getAuthors();
%>
<body>
    <table>
        <tr>
            <th></th>
            <th>Name</th>
            <th>Born</th>
            <th>Died</th>
        </tr>

        <c:forEach var="author" items="${authors}">
        <p>hello</p>/* not showing */
            <tr>
                <td>${authors.id}</td>
                <td><a href="author.jsp">${author.name}</a></td>
                <td>${author.born}</td>
                <td>${author.died}</td>
            </tr>
        </c:forEach>
    </table>

</body>
</html>

But somehow whatever I put inside the <c:forEach> tag, it doesn't show up on the actual page (including the testing

tag). I have the jar file of jstl 1.2 in WEB-INF/lib directory, so I don't know what is going on.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Liumx31
  • 1,190
  • 1
  • 16
  • 33

2 Answers2

1

FIRST OF ALL check this answer first!

How to avoid Java code in JSP files?


Your getting the data, but not setting attribute, thus <for:each> cannot iterate over nothing.

<%
    DataManager dm = (DataManager) application.getAttribute("datamanager");  // get datamanager
    ArrayList<DataManager.Author> authors = dm.getAuthors();  // get authors
    // missing put authors in session!!!
%>

Add

request.setAttribute("authors", authors);

And will works as you expect.

Community
  • 1
  • 1
joc
  • 1,336
  • 12
  • 31
1

add below code in your java code.

request.setAttribute("authors", authors);
Ganse
  • 101
  • 6