我正在尝试使用 JSP/JSTL 遍历随机生成的整数的 myList (数组)。我的生成和存储整数的代码片段位于我的 servlet 中。
另一方面,遍历字符串的arrayList(参见下面的代码)可以完美地工作,但是当我尝试使用基于相同逻辑的数组时,我的网页只是没有显示我的随机整数的任何无序列表。
谢谢你帮我
我的小服务程序
package be.intec.servlets;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import be.intecbrussel.entities.Auto;
@WebServlet("/JSTLServlet")
public class JSTLServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String VIEW = "WEB-INF/JSP/JSTL.jsp";
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher(VIEW);
//=======below is the code using Array=====================================
int[] myList = new int[42];
for (int i = 0; i < myList.length; i++) {
myList[i] = (int) (Math.random() * 100);
}
request.setAttribute("mylist", myList);
//=======below is the code using ArrayList=====================================
List<String> names = Arrays.asList("John", "Georges", "Kevin");
request.setAttribute("names", names);
dispatcher.forward(request, response);
}
}
我的 jstl.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"%>
<%@ 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=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="styles/default.css">
<title>JSTL Expample</title>
</head>
<body>
<h2>Iterate through my array</h2>
<ul>
<c:forEach var="arrayVar" items="${myList}">
<li>${arrayVar}</li>
</c:forEach>
</ul>
<!-- ================================================================================ -->
<h2>Iterate through my arrayList</h2>
<ul>
<c:forEach var="name" items="${names}">
<li>${name}</li>
</c:forEach>
</ul>
</body>
</html>
我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ServletsAndJSPExampleProject</display-name>
<welcome-file-list>
<welcome-file>IndexServlet</welcome-file>
</welcome-file-list>
</web-app>
我的索引 Servlet
package be.intec.servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String VIEW = "/WEB-INF/JSP/index.jsp";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher(VIEW);
dispatcher.forward(request, response);
}
}
浏览器中的输出是:
遍历我的数组://这里应该显示我的随机数
遍历我的arrayList://工作得很好
约翰
乔治斯
凯文