gpt4 book ai didi

java - JSTL - 迭代随机整数数组 - 不显示在网页中

转载 作者:行者123 更新时间:2023-12-01 07:33:15 25 4
gpt4 key购买 nike

我正在尝试使用 JSP/JSTL 迭代随机生成的整数的 myList(数组)。我的代码片段生成并存储整数,位于我的 servlet 中。

另一方面,迭代字符串数组列表(参见下面的代码)效果很好,但是当我尝试使用基于相同逻辑的数组时,我的网页只是不显示任何随机整数的无序列表。

谢谢你帮助我

我的Servlet

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://工作得很好

  • 约翰

  • 乔治

  • 凯文

最佳答案

您已在 Servlet 中使用“mylist”作为名称,并希望使用 ${myList} 获取列表。 名称区分大小写!

关于java - JSTL - 迭代随机整数数组 - 不显示在网页中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15522149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com