java - 从 Controller 到 jsp 的对象列表和 <c :forEach>

标签 java spring jsp spring-mvc

我尝试从 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/

相关文章:

java - 我收到“存在意外错误(类型=未找到,状态=404)”错误?

java - 结果集分页

java - 编写单个 servlet 来添加/编辑/删除项目以及列出项目

java - 使用 JRE 5/6 运行 Axis2 v.1.6.2 生成的客户端需要哪些 jar?

java - 如何修复这个错误的操作数 (&&)

spring - Spring Data Elasticsearch-在查询的根级别上是否支持Inner Hit?

java - Eclipse 中运行的 Tomcat 实例的 "work"目录在哪里?

java - byte byteVar = 100 有效,但 int intVar = 100L 会导致编译错误。为什么?

java - Spring RestTemplate 字符集 UTF-8 不起作用

spring - 如何推迟调用@PostConstruct,直到jUnit设置测试上下文