0

JSP/JSTL を使用して、ランダムに生成された整数の myList (配列) を反復処理しようとしています。整数を生成して格納するコード スニペットは、サーブレットにあります。

一方、文字列のarrayList(以下のコードを参照)の反復処理は完全に機能しますが、同じロジックに基づいて配列で試してみると、Webページにランダムな整数の順序付けられていないリストが表示されません.

私を助けてくれてありがとう

私のサーブレット

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>

私のインデックスサーブレット

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を反復処理します://非常にうまく機能します

  • ジョン

  • ジョルジュ

  • ケビン

4

2 に答える 2

5

サーブレットで名前として" mylist" を使用しており、 を使用してリストを取得したいと考えています${myList}名前は大文字と小文字が区別されます。

于 2013-03-20T11:32:55.470 に答える
2

以下に示すように、 for each を変更します。

<c:forEach var="arrayVar" items="${mylist}">
<li>${arrayVar}</li>
</c:forEach>
于 2013-03-20T11:39:32.557 に答える