0

I try to send list of Employee from Spring MVC controller and then use it in jsp with forEach loop but receive blank page.

What is wrong?

Here is my code:

@RequestMapping(value = "getuserbylastname", method = RequestMethod.GET)
Employee employee, ModelMap model) {
public ModelAndView searchUserByLastname(@ModelAttribute("employee")Employee employee, ModelMap model) {
    List emplList = new ArrayList();
    EmployeeDAO employeeDAO = new EmployeeDAOImpl();
    emplList = employeeDAO.getByLastname(employee.getLastName());// here list size is 2
    model.addAttribute("listOfEmployees", emplList);
    return new ModelAndView("redirect:/searchbysurnameresult", "Model", emplList);
}

jsp:

<html>
 <body>
 <c:forEach var="employee" items="${Model}">
   <c:out value="${employee.firstName}"/>
 </c:forEach>
 </body>
</html>

P.S.I have a taglib url in JSP but there is a problem to display it like code at this site

Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
Emanon
  • 33
  • 6

2 Answers2

2

When you redirect your page this issue occurs and the data present in your model attribute becomes null. Spring MVC has added new type of attribute said as FlashAttributes. Use them to solve this issue.

This link has more information on how to use Flash attributes.

Community
  • 1
  • 1
Vivek Singh
  • 2,047
  • 11
  • 24
1

this works:

    @RequestMapping(value = "getuserbylastname", method = RequestMethod.GET)
    public RedirectView searchUserByLastname(@ModelAttribute("employee") Employee employee, RedirectAttributes redirectAttrs) {
      List emplList = new ArrayList();
      EmployeeDAO employeeDAO = new EmployeeDAOImpl();
      emplList = employeeDAO.getByLastname(employee.getLastName());
      redirectAttrs.addFlashAttribute("listOfEmployees", emplList);
      return new RedirectView("searchbysurnameresult", true);
}
Emanon
  • 33
  • 6