繁体   English   中英

如何通过servlet在jsp中显示用户定义的arraylist,如下所示。 我已经创建了用户定义的arraylist并传递给了jsp

[英]how to display user defined arraylist in jsp via servlet just like below. i have created user defined arraylist and passed to the jsp

我应该怎么做? 使用桌子吗? 你能举个例子吗? 我刚来这地方! 帮助将不胜感激。输出应如下所示:

2018-07-30        2018-07-31     2018-08-01
min temp:27       min temp:30    min temp:28
max temp:29       max temp:34    max temp:32
humidity:74       humidity:70    humidity:75

在Forecast.jsp页面中

  <c:forEach var="weather" items="${weatherList}">                       
         <c:out value="${weather.date}"/><br> 
         <c:out value="${weather.minTemp}"/><br>
         <c:out value="${weather.maxTemp}"/><br>
         <c:out value="${weather.humidity} "/><br>
        </c:forEach>        

在Servlet类中,im从数据库获取值并将其添加到arraylist

     ArrayList<WeatherDataDto> weatherList = new ArrayList<>();
        try {
            String query = "select * from weather_data where date>='2018-07-30'";
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);    
            while (rs.next()) {
                String date = rs.getString(1);
                int minTemp = rs.getInt(2);
                int maxTemp = rs.getInt(3);
                int humidity = rs.getInt(4);
                WeatherDataDto weatherDto = new WeatherDataDto(date, minTemp, maxTemp, humidity);
                weatherList.add(weatherDto);
        }
            rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

request.setAttribute("weatherList", weatherList);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/forecast.jsp");
        dispatcher.forward(request, response);

//这是我的bean类

public class WeatherDataDto {

    private String date;
    private int minTemp;
    private int maxTemp;
    private int humidity;


    public WeatherDataDto(){        
    }

    public WeatherDataDto(String date, int minTemp,int maxTemp, int humidity) {
        super();
        this.date = date;
        this.minTemp = minTemp;
        this.maxTemp = maxTemp;
        this.humidity = humidity;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getMinTemp() {
        return minTemp;
    }

    public void setMinTemp(int minTemp) {
        this.minTemp = minTemp;
    }

    public int getMaxTemp() {
        return maxTemp;
    }

    public void setMaxTemp(int maxTemp) {
        this.maxTemp = maxTemp;
    }

    public int getHumidity() {
        return humidity;
    }

    public void setHumidity(int humidity) {
        this.humidity = humidity;
    }

我只想显示上述格式的清单。 帮帮我

<table>
          <c:forEach var="weather" items="${weatherList}" varStatus="index">                       
            <tr>
              <c:forEach begin="0" end="${fn:length(weatherList)" varStatus="loop">
                 <td><c:out value="${weatherList[loop].date}"/></td> 
              </c:forEach>
            </tr>
            <tr>
              <c:forEach begin="0" end="${fn:length(weatherList)" varStatus="loop">
                 <td><c:out value="${weatherList[loop].minTemp}"/></td> 
              </c:forEach>
            </tr>
           <tr>
              <c:forEach begin="0" end="${fn:length(weatherList)" varStatus="loop">
                 <td><c:out value="${weatherList[loop].maxTemp}"/></td> 
              </c:forEach>
            </tr>
          <tr>
              <c:forEach begin="0" end="${fn:length(weatherList)" varStatus="loop">
                 <td><c:out value="${weatherList[loop].humidity}"/></td> 
              </c:forEach>
            </tr>
        </c:forEach> 
        </table>

尝试这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM