[英]How to iterate through an ArrayList of Objects of ArrayList of Objects?
[英]How to iterate through ArrayList of Objects of ArrayList of Object and displaying the data inside a form of JSP Page in Spring boot?
我正在从名为 Cars 的表中获取数据(获取特定品牌的模型,一个品牌可以有多个模型)。 选择品牌后,我想在 JSP 页面的表格中显示所有型号及其详细信息。 数据是 ArrayList 的 object 对象的 ArrayList,我想迭代它并在我的 Z2D7FCA4F5778E9BB29B46 页面上显示每个字段。
存储库:
@Repository
public interface CarsRepository extends JpaRepository<Cars, Integer> {
@Query("select c from Cars c where c.brand = ?1")
public List<Cars> getModels(String brandName);
}
Controller:
@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 + "]";
}
}
为什么不使用 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,您可以使用 JSTL 通过在您的页面中包含 JSTL 来迭代您的 model 属性<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
并使用c:foreach
标签。 例如:
<c:forEach var="car" items="${models}">
<c:out value="${car.brand}"/>
</c:forEach>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.