简体   繁体   English

数据库数据未在视图中使用 spring 数据 JPA 和 springboot 访问

[英]Database data not accessing in views using spring data JPA with springboot

I am trying to display my database data using spring data JPA with springboot.我正在尝试使用带有 springboot 的 spring 数据 JPA 显示我的数据库数据。 The following is my code, this is my controller file,以下是我的代码,这是我的控制器文件,

@Autowired
DriverRepository driverRepo;
@RequestMapping(value = "/dHome", method = RequestMethod.GET)
public   ModelAndView driverLoad()
{
Driver driverDetails = new Driver();
driverDetails =  (Driver) driverRepo.findAll();
ModelAndView model = new ModelAndView("driverhome");
return model;
}

And the following is my view file,以下是我的视图文件,

<c:forEach var="list" items="${driverDetails}">
<c:out value="${list.name}"/>
<c:out value="${list.age}"/>
</c:forEach>

And I am getting the result like "There was an unexpected error (type=Internal Server Error, status=500). java.util.ArrayList cannot be cast to com.central.model.Driver"我得到的结果是“出现意外错误(类型=内部服务器错误,状态=500)。java.util.ArrayList 无法转换为 com.central.model.Driver”

First of all, when you use findAll method, it returns List.首先,当您使用 findAll 方法时,它返回 List。

List< Driver> driverDetails = new ArrayList< Driver>(); List<Driver> driverDetails = new ArrayList<Driver>(); driverDetails = (List< Driver>) driverRepo.findAll(); driverDetails = (List<Driver>) driverRepo.findAll();



You also need to add driverDetails to model.addAttribute() This should work.您还需要将 driverDetails 添加到 model.addAttribute() 这应该可以工作。 Do some research on how to send a model to a view using ModelAndView研究如何使用 ModelAndView 将模型发送到视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在springboot和Data JPA中使用nativeQuery方法访问数据记录 - Can not accessing data records using nativeQuery method in springboot and Data JPA 使用 Springboot 在 Oracle、MySQL 数据库中创建用户 - Spring Data JPA - Create users in Oracle, MySQL databases using Springboot - Spring Data JPA 无法在SpringBoot应用程序中使用JPA在数据库中保存数据 - Unable to save data in database using JPA in SpringBoot application Spring JPA @事务性数据访问 - Spring JPA @Transactional data accessing 分页-使用Spring Data JPA访问第二页时出现UnsupportedOperationException - Pagination - UnsupportedOperationException while accessing to the second page using Spring Data JPA 使用 spring 数据从关系数据库添加和检索数据 jpa - Adding and retrieving the data from relational database using spring data jpa Spring Data JPA在mysql视图上支持PagingandSorting吗? - Spring data jpa supports PagingandSorting on mysql views? Spring 数据 jpa 表视图之间的操作 - Spring data jpa operation between table views 访问过滤器中的spring boot数据jpa实体 - Accessing spring boot data jpa entity in filter 如何使用SpringBoot使用Spring Data JPA和MYSQas DB创建一个简单的CRUD应用程序? - How to create a simple CRUD application using Spring Data JPA and MYSQas DB using SpringBoot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM