c:forEach似乎找不到请求范围中的列表

时间:2017-02-08 13:34:59

标签: jsp jstl

我正在学习如何使用Java Servlets,我已经设置了一个玩具示例。

我在servlet方法中有doGet以下逻辑:


@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //HttpSession session = request.getSession();
        List theList = new ArrayList();
        theList.add(1);
        theList.add(2);
        theList.add(3);
        request.setAttribute("intList", theList);

        RequestDispatcher dispatcher = request.getRequestDispatcher("hello.jsp");        
        dispatcher.forward(request, response);
    }

然后我在hello.jsp中有以下代码:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>

<h1>Redirect Worked</h1>
<c:forEach items="${intList}" var="item">
    ${item}<br>
</c:forEach>
</body>
</html>

我希望我的浏览器显示:

Redirect Worked
1
2
3

但我所看到的只是:

Redirect Worked

我做错了什么?

2 个答案:

答案 0 :(得分:0)

也许这样的事情会起作用吗?

    <c:forEach items="${intList}" var="item">
        <c:out value="${item}"/>
    </c:forEach>

答案 1 :(得分:0)

感谢DaveH,theodosis和Chaim我修复了我的JSP文件:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>redirect worked!</h1>
    <c:forEach var="item" items="${requestScope.intList}">
        <c:out value="${item}"/>
    </c:forEach>

</body>
</html>