首页 > 解决方案 > 如何在Spring boot中遍历Object的ArrayList的ArrayList并在JSP Page的表单中显示数据?

问题描述

我正在从名为 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 + "]";
    }
    
}

标签: javaspring-bootspring-mvcjspspring-data-jpa

解决方案


为什么不使用 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);

推荐阅读