我试图在通过 Controller 返回的 jsp 页面中显示该对象,但我没有在 jsp 中看到该对象。下面是我的 Controller :
@RequestMapping(value = "/search/{groupName}", menthod = {RequestMethod.Get, RequestMethod.POST})
public ModelAndView groupAlphaHandler(@PathVariable("groupName") String groupName, HttpServletRequest request) {
ArryList<GroupAlphaInfoVO> groupAlphaInfoVO = groupAlphaService.loadGroupAlphaSearchResult(groupName);
//view name "group-alpha"
ModelAndView mav = new ModelAndView("group-alpha");
mav.addObject("groupAlphaInfoVO", groupAlphaInfoVO);
mav.addObject("pageTitle", "Group Alpha");
//added debug point here and made sure groupAlphaInfoVO is not null (it has around 1000 records)
return mav;
}
这是我的jsp页面group-alpha.jsp:
<html>
<head>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Group Alpha</title>
</head>
<body>
${pageTitle}<!-- this is getting displayed on jsp-->
${groupAlphaInfoVO}
</body>
</html>
最佳答案
看来你在 Controller 中犯了一个愚蠢的错误
@RequestMapping(value = "/search/{groupName}", menthod = {RequestMethod.Get, RequestMethod.POST})
有一个拼写错误。这不是**menthod**
这是**method**
而且我认为没有必要使用 RequestMethod.POST
另一个错误是mav.addObject("groupAlphaInfoVO", groupAlphaInfoVO);
使用此代码,您将放置对象列表。并且在JSP页面中你没有对列表进行任何操作。要打印该列表,您应该写 <c:forEach>....</c:forEach>
代码。例如
<c:forEach var="results" items="${groupAlphaInfoVO}">
<c:out value="${results.userid}"></c:out>
<c:out value="${results.password}"></c:out>
<c:out value="${results.role}"></c:out>
<c:out value="${results.contact}"></c:out>
<c:out value="${results.mentor}"></c:out>
<c:out value="${results.group}"></c:out>
</c:forEach>
要使用,您需要在 pom.xml 中添加 JSTL 依赖项
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
并添加<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
在 jsp 页面的顶部。
关于java - ModelAndView 不将模型返回到 spring mvc 中的 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51296131/