1

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"

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.