Java JSTL-遍历随机整数数组-不显示在网页中
我试图使用JSP/JSTL遍历随机生成的整数的myList数组。 我的代码snipet生成并存储整数,它位于我的servlet中 另一方面,遍历字符串数组列表see code可以很好地工作,但是当我尝试使用基于相同逻辑的数组时,我的网页上没有显示任何无序的随机整数列表 谢谢你帮助我 我的ServletJava JSTL-遍历随机整数数组-不显示在网页中,java,jsp,servlets,jstl,Java,Jsp,Servlets,Jstl,我试图使用JSP/JSTL遍历随机生成的整数的myList数组。 我的代码snipet生成并存储整数,它位于我的servlet中 另一方面,遍历字符串数组列表see code可以很好地工作,但是当我尝试使用基于相同逻辑的数组时,我的网页上没有显示任何无序的随机整数列表 谢谢你帮助我 我的Servlet package be.intec.servlets; import java.io.IOException; import java.math.BigDecimal; i
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);
}
}
浏览器中的输出为:
遍历我的数组:
//这里应该显示我的随机数
遍历我的arrayList:
//工作得很好
约翰
乔治
凯文
您已经在Servlet中使用mylist作为名称,并且希望使用${mylist}获取列表。名称区分大小写 如下所示,分别更改您的:
<c:forEach var="arrayVar" items="${mylist}">
<li>${arrayVar}</li>
</c:forEach>
谢谢,我注意到了这一点,尽管我知道jsp/jstl中的大小写敏感性,但我并不认为在这个特定的案例中需要它。感谢我愚蠢的mystake。这确实是另一种选择,但我将保留myList的正字法,以尊重命名约定。谢谢你的帮助
<?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>
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);
}
}
<c:forEach var="arrayVar" items="${mylist}">
<li>${arrayVar}</li>
</c:forEach>