0

为什么在JSTL中使用时ArrayList需要放在a中而不是在同一个jsp中时需要放在普通字符串数组中?request.setAttt(...)

ArrayList<String> list = new ArrayList();  

     while (r.next()) {
               list.add(r.getString("stu_first_name"));
         }
        <c:forEach items="${list}" var="names">
        <tr>
        <td>${names}</td><br/>
        </tr>
        </c:forEach> 

更改为

String[] list = new String[x] 

你不需要

request.setAttribute("list", list); 

为了

<c:forEach to work>

必须使用${directive.list}它才能工作吗?

4

1 回答 1

0
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<%
        String[] colors = {"Red", "Green", "Blue"};
        //ArrayList<String> arrcolors = new ArrayList();
        //colors must be set to request for JSTL to access in code line.
        request.setAttribute("colors", colors);


    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>EL Page</title>
    </head>
    <body>
        <form name="tester" action="post" method="/dropentry">
            <select>
            <c:forEach items="${colors}" var="names"> 
                <option value="${names}">${names}</option>
             </c:forEach>   
        </select>
            <input type="submit" value="Enter James"
        </form>
于 2013-11-10T22:27:36.660 回答