如何在jsp中使用foreach进行迭代

https://img1.sycdn.imooc.com/653fa1170001b9e805160399.jpg

以上是我正在尝试创建的内容


你好,


以上是我尝试使用 Spring Boot 创建的内容。我的问题是如何正确地迭代 forEach 循环并提取 JSP 中所需的必要数据?我以为我做得正确,但是,我不断收到错误消息“不知道如何迭代 <forEach> 中提供的“items””


请参阅下面的代码了解我目前正在做什么。


提前感谢您的帮助!


控制器


@RequestMapping(value = "/showOrders.html")

    public String listOrders(Model model) {


        ArrayList<Order> orders = os.findAll();

        model.addAttribute("allOrders", orders);



        return "allOrders";

    }

“订单”的服务类别


package com.sales.services;


import java.util.ArrayList;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


import com.sales.models.Order;

import com.sales.repositories.OrderRepository;

@Service

public class OrderService {


    @Autowired

    OrderRepository or;


    public ArrayList<Order> findAll() {

        return (ArrayList<Order>) or.findAll();


    }


}

订单的存储库类


package com.sales.repositories;


import java.util.ArrayList;

import java.util.List;


import org.springframework.data.jpa.repository.Query;

import org.springframework.data.repository.CrudRepository;

import org.springframework.data.repository.query.Param;

import org.springframework.stereotype.Repository;


import com.sales.models.Customer;

import com.sales.models.Order;


@Repository

public interface OrderRepository extends CrudRepository<Order, Long> {




}


桃花长相依
浏览 79回答 1
1回答

有只小跳蛙

下面是这个问题的解答<%@ page language="java" contentType="text/html; charset=ISO-8859-1"&nbsp; &nbsp; pageEncoding="ISO-8859-1"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html><html><link href="/css/style.css" rel="stylesheet"></link><head><meta charset="ISO-8859-1"><title>List of Customers</title></head><body>&nbsp; &nbsp; <h1>List of Customers</h1>&nbsp; &nbsp; <c:forEach items="${ordrs}" var="ords">&nbsp; &nbsp; &nbsp;<c:forEach items="${ords.orders}" var="order">&nbsp; &nbsp; &nbsp; &nbsp; <b>${order.oId} </b>&nbsp; &nbsp; &nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Quantity</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Order Date</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Customer ID</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Customer Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Product ID</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Description</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>${order.qty}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>${order.orderDate}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>${ords.cId}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>${ords.cName}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>${order.prod.pId}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>${order.prod.pDesc}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; </c:forEach>&nbsp; &nbsp; </c:forEach></body></html>`````````````````````````````````````````````````````````````````````````````````````````
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5