java - 如何在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 + "]";
}
}
解决方案
为什么不使用 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);
推荐阅读
- java - 在java中删除元素表单链表,意外行为
- database - ClickHouse 副本不同步
- python - 为什么 {% if post.is_liked %} 在 home.html 中不起作用
- javascript - 将二进制文件的存储字节数组读取为文本文件中的逗号分隔值
- node.js - typescript express get 在单独的文件中不起作用
- java - Java:枚举类构造函数
- python - 用于 Python 的 Pandas DataFrame
- php - Neo4j PHP Graphaware '415 不支持的媒体类型'
- javascript - 使用带有下拉菜单的 v-for 对先前渲染和匹配的列表进行排序
- c++ - 如何基于元组元素进行递归?