ModelAndView 未将模型返回到 spring mvc 中的 JSP
ModelAndView not returning model to the JSP in spring mvc
我试图在通过控制器返回的 jsp 页面中显示该对象,但我在 jsp 中看不到该对象。下面是我的控制器:
@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>
你需要这个isELIgnored="false"
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%> //here
看来你在控制器中犯了一个愚蠢的错误
@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>
并在 jsp 页面顶部添加 <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
。
我试图在通过控制器返回的 jsp 页面中显示该对象,但我在 jsp 中看不到该对象。下面是我的控制器:
@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>
你需要这个isELIgnored="false"
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%> //here
看来你在控制器中犯了一个愚蠢的错误
@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>
并在 jsp 页面顶部添加 <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
。