Jsp c:forEach似乎在请求范围中找不到列表
我正在学习如何使用Javaservlet,并设置了一个玩具示例 我有一个Jsp c:forEach似乎在请求范围中找不到列表,jsp,jstl,Jsp,Jstl,我正在学习如何使用Javaservlet,并设置了一个玩具示例 我有一个servlet,在doGet方法中具有以下逻辑: @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //HttpSession session = request.get
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>
JSP页面
重定向成功了
${item}
我希望我的浏览器显示:
Redirect Worked
1
2
3
重定向成功了
1.
2.
3.
但我看到的是:
Redirect Worked
重定向成功了
我做错了什么?也许像这样的方法会奏效
<c:forEach items="${intList}" var="item">
<c:out value="${item}"/>
</c:forEach>
多亏了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>
JSP页面
重定向成功了!
我已经很久没有使用jsp了,但是您不需要在jsp中包含jstl库吗<代码>请参阅您的两个建议的组合,以修复此问题