Search code examples
javaspring-mvcjspjsp-tags

ModelAndView not returning model to the JSP in spring mvc


I am trying to display the object in a jsp page that are returned via a controller, but I am not seeing the object in the jsp. Below is my 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;
    }

Here is my jsp page 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>


Solution

  • Seems you are making a silly mistake in the controller

    @RequestMapping(value = "/search/{groupName}", menthod = {RequestMethod.Get, RequestMethod.POST})
    

    There is a spelling mistake. It's not **menthod** it's **method** And I think there is no need for using RequestMethod.POST

    Another mistake is mav.addObject("groupAlphaInfoVO", groupAlphaInfoVO); with this code you are putting list of object. And in the JSP page you didn't do any operation on the list. To print that list you should write <c:forEach>....</c:forEach> code. For example

    <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>
    

    For using you need to add JSTL dependency in your pom.xml

    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    

    And add <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> at the top of your jsp page.