Question

I am sending Object with help of ModdelAndView in Spring controller, but i am not able to read it on jsp?

JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    Tried with EL:

    ${user}

    Try with JSTL:

    <c:out value="${user}"></c:out>

</body>
</html>

controller:

@Controller
public class StudentHome {
    @RequestMapping(value = "/auth/Home")
    public ModelAndView RedirectLogin() {
        ModelAndView modelAndView = new ModelAndView("/auth/Home");
        modelAndView.addObject("user", "Alex");
        return modelAndView;
    }
}

I have tried both Spring EL and jstl its not working. Do i need to include anything else?

Was it helpful?

Solution

We need to include org.springframework.web.servlet.ModelAndView instead of org.springframework.web.portlet.ModelAndView;

OTHER TIPS

The model presents a placeholder to hold the information you want to display on the view. It could be a string, which is in your above example, or it could be an object containing bunch of properties.

update your code as follows

@Controller
public class StudentHome {
    @RequestMapping(value = "/auth/Home")
    public ModelAndView RedirectLogin() {
        return new ModelAndView("yourJspName","user", "Alex");
    }
}

then in your jsp, to display the message, you will do

Hello ${user}!

hope this will help you..!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top