, be careful not to add a generic type when defining list."/> , be careful not to add a generic type when defining list.">
Home >Web Front-end >Front-end Q&A >How to create a list in jsp
You can use this72637aecae1027e7d023ac098a170986, which means that the Java code is written in it. The other definitions are the same as the Java code.
For example, define a string type array
<% String[] datas = new String[5]; %>
However, if you want to define a list, you need to add
0ae99eee2bd2f76e5f6826ee24e8ce7aSimilar to the java code in the jsp page Import the referenced class
<% List list = new ArrayList(); %>
It is best not to add a generic
Case: Traversing the LIST list in JSP
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% List<String> list = new ArrayList<String>(); list.add("简单是可靠的先决条件"); list.add("兴趣是最好的老师"); list.add("知识上的投资总能得到最好的回报"); request.setAttribute("list", list); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Jsp使用c:forEach遍历List集合</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <b>遍历List集合的全部元素:</b> <br> <c:forEach items="${requestScope.list}" var="keyword" varStatus="id"> ${id.index} ${keyword}<br> </c:forEach> <br> <b>遍历List集合中第一个元素以后的元素(不包括第一个元素):</b> <br> <c:forEach items="${requestScope.list}" var="keyword" varStatus="id" begin="1"> ${id.index} ${keyword}<br> </c:forEach> </body> </html>
The above is the detailed content of How to create a list in jsp. For more information, please follow other related articles on the PHP Chinese website!