【问题标题】:How to iterate through ArrayList of Objects of ArrayList of Object and displaying the data inside a form of JSP Page in Spring boot?java - 如何在Spring Boot中遍历Object的ArrayList的ArrayList并在JSP Page的表单中显示数据?
【发布时间】:2021-08-30 05:51:12
【问题描述】:

我正在从名为 Cars 的表中获取数据(获取特定品牌的模型,一个品牌可以有多个模型)。选择品牌后,我想在 JSP 页面的表单中显示所有模型及其详细信息。 数据是对象的 ArrayList 的对象的 ArrayList,我想对其进行迭代并在我的 JSP 页面上显示每个字段。

存储库:

@Repository
    
     public interface CarsRepository extends JpaRepository<Cars, Integer> {
    
     @Query("select c from Cars c where c.brand = ?1")
     public List<Cars> getModels(String brandName);
     }

控制器:

 @RestController
    
        public class CarsRestController {
        
        
          private CarsService carsService;
          
          @Autowired public CarsRestController(CarsService theCarsService) {
             carsService = theCarsService; 
          }
         
         @RequestMapping("/")
         public ModelAndView homePage() {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("home");
            return mv;
        }
        
        
         @RequestMapping("/searchModels")
         public ModelAndView modelsOfBrand(@RequestParam("brandName") String theBrand, Model theModel) {
            List<Cars> modelDetails = carsService.getModels(theBrand);
    //       for(int i=0; i < modelDetails.size(); i++) {
    //           System.out.println(modelDetails.get(i));
    //           theModel.addAttribute("models", modelDetails.get(i));
    //       }
            
            theModel.addAttribute("models", modelDetails);
            theModel.addAttribute("size", modelDetails.size());
            return new ModelAndView("modelDetailsPage");
        }

实体:

package com.bcc.myspringbootdemo.bestchoicecars.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="cars")
public class Cars {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;
    
    @Column(name="brand")
    private String brand;
    
    @Column(name="model")
    private String model;
    
    @Column(name="year")
    private int year;
    
    @Column(name="no_of_kms")
    private int kms;
    
    @Column(name="price")
    private int price;
    
    @Column(name="fuel")
    private String fuel;
    
    public Cars() {
        
    }

    public Cars(String brand, String model, int year, int kms, int price, String fuel) {
        this.brand = brand;
        this.model = model;
        this.year = year;
        this.kms = kms;
        this.price = price;
        this.fuel = fuel;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getKms() {
        return kms;
    }

    public void setKms(int kms) {
        this.kms = kms;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getFuel() {
        return fuel;
    }

    public void setFuel(String fuel) {
        this.fuel = fuel;
    }

    @Override
    public String toString() {
        return "Cars [id=" + id + ", brand=" + brand + ", model=" + model + ", year=" + year + ", kms=" + kms
                + ", price=" + price + ", fuel=" + fuel + "]";
    }
    
}

【问题讨论】:

    标签: java spring-boot spring-mvc jsp spring-data-jpa


    【解决方案1】:

    为什么不使用 foreach 循环?

    List<Cars> cars = carsService.getModels(theBrand);
    List<String> models = new ArrayList<>();
    for(Car car : cars) {
        models.add(car.getModel());
        // any other property.
    }
    theModel.addAttribute("model", models);
    

    【讨论】:

    • 如何在 JSP 页面中对其进行迭代? 在 JSP 页面中访问时,名为 'model' 的属性将保存最后一次迭代的值。假设我通过getModel()得到了3个模型,A,B和C,如何通过这个属性保存所有这些值并在JSP Page中访问。
    • 我已经更新了代码。但是你想怎么在jsp页面上查看呢,你只需要在jsp页面上编码就行了。
    【解决方案2】:

    如果您使用 JSP,您可以使用 JSTL 来迭代您的模型属性,方法是在您的页面 &lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt; 中包含 JSTL 并使用 c:foreach 标记。例如:

    <c:forEach var="car" items="${models}">
        <c:out value="${car.brand}"/>
    </c:forEach>
    

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 2014-06-14
      • 2015-06-27
      • 1970-01-01
      • 2017-10-18
      • 2015-04-15
      • 2019-04-19
      • 1970-01-01
      • 2016-08-21
      相关资源
      最近更新 更多