【问题标题】:JSTL c:forEach does not see variable declared in scriptlet [duplicate]JSTL c:forEach 看不到 scriptlet 中声明的变量 [重复]
【发布时间】:2016-08-25 06:49:05
【问题描述】:

所以我有一个简单的 JSP 页面来显示这样的一些信息:

<%@ 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>

但不知何故,无论我在&lt;c:forEach&gt; 标签内放什么,它都不会显示在实际页面上(包括测试

标签)。我在WEB-INF/lib目录下有jstl 1.2的jar文件,所以不知道怎么回事。

【问题讨论】:

  • 你在类路径中添加了jstl库吗?
  • 可能是因为它在表内但不在 td 中的行之间

标签: jsp jstl el


【解决方案1】:

首先 先检查这个答案!

How to avoid Java code in JSP files?


您获取数据,但未设置属性,因此 &lt;for:each&gt; 无法迭代任何内容。

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

添加

request.setAttribute("authors", authors);

并且会按您的预期工作。

【讨论】:

    【解决方案2】:

    在您的 java 代码中添加以下代码。

    request.setAttribute("authors", authors);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-20
      • 2020-07-14
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      相关资源
      最近更新 更多