简体   繁体   中英

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. 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"

First of all, when you use findAll method, it returns List.

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



You also need to add driverDetails to model.addAttribute() This should work. Do some research on how to send a model to a view using ModelAndView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM