我尝试从 Spring MVC Controller 发送 Employee 列表,然后在 jsp 中使用 forEach 循环使用它,但收到空白页面。
出了什么问题?
这是我的代码:
@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.我在 JSP 中有一个 taglib url,但在该网站上像代码一样显示它时出现问题
最佳答案
当您重定向页面时,会出现此问题,并且模型属性中存在的数据变为空。 Spring MVC
添加了新类型的属性,称为 FlashAttributes
。使用它们来解决这个问题。
这个link有关如何使用 Flash 属性的更多信息。
关于java - 从 Controller 到 jsp 的对象列表和 <c :forEach>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34150925/