I would like to display my List of values in my jsp view, but i am not able to do this.
Here is my controller class below, which is only add List to the ModelAndView
map and than it redirects to my index.jsp
page.
EmployeeController
@Controller
public class EmployeeController {
@RequestMapping(value={"/employee"}, method = RequestMethod.GET)
public String listEmployee(){
System.out.println("Kontroler EmployeeController");
LinkedList<String> list = getList();
ModelAndView map = new ModelAndView("index");
map.addObject("lists", list);
return map.getViewName();
}
private LinkedList<String> getList(){
LinkedList<String> list = new LinkedList<>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
return list;
}
}
index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome to Spring Web MVC project</title>
</head>
<body>
<h1>Index page</h1>
<h1>${msg}</h1>
<a href="/MavenHello/employee">Zaměstnanci</a>
</body>
<c:if test="${not empty listEmployee}">
<ul>
<c:forEach var="listValue" items="${listEmployee}">
<li>${listValue}</li>
</c:forEach>
</ul>
</c:if>
I am able to access the controller, because every time I hit "Zaměstnanci"
, the System.out.println("Kontroler EmployeeController")
prints "Kontroler EmployeeController"
in to Tomcat Log, but the index.jsp
page is blank.
Please, can someone give me an advice?
As you are populating ModelAndView
return ModelAndView itself and not map.getViewName()
which return only name of the name without data as stated in docs:
public String getViewName() Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if we are using a View object.
as follows:
@RequestMapping(value = { "/employee" }, method = RequestMethod.GET)
public ModelAndView listEmployee() {
System.out.println("Kontroler EmployeeController");
LinkedList<String> list = getList();
ModelAndView map = new ModelAndView("index");
map.addObject("lists", list);
return map;
}
Secondly, you are missing jstl tag <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
on your index page and variable name you given to list is "lists" so iterate over "lists" rather than "listEmployee", as follows:
<html>
<head>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome to Spring Web MVC project</title>
</head>
<body>
<h1>Index page</h1>
</body>
<c:if test="${not empty lists}">
<c:forEach items="${lists}" var="lists">
${lists}
</c:forEach>
</c:if>
Also, make sure you have JSTL dependency in your classpath:
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Just add Model
to your controller method params and then add attribute to this model.
RequestMapping(value={"/employee"}, method = RequestMethod.GET)
public String listEmployee(Model model){
System.out.println("Kontroler EmployeeController");
LinkedList<String> list = getList();
model.addAttribute("lists", list);
return "index";
}
One more way to do this, is to return ModelAndView
instead of String
@RequestMapping(value={"/employee"}, method = RequestMethod.GET)
public ModelAndView listEmployee(){
System.out.println("Kontroler EmployeeController");
LinkedList<String> list = getList();
ModelAndView map = new ModelAndView("index");
map.addObject("lists", list);
return map;
}
Just choose which way is better for you.
And also change listEmployee
in your index page to lists
as in the ModelAndView
you have an attribute as lists
not listEmployee`.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With