Capril Aprilovich
Capril Aprilovich

Reputation: 435

<c:foreach> jstl tag doesn't print anything

The tag doesn't work... Jsp doesn't print anything... Could I debug jsp page in Eclipse? view

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <ul>
        <c:forEach items="${listOfMyFriends}" var="friend">
        <c:out value="${friend}"></c:out>
        </c:forEach>

    </ul>
</body>
</html>

controller are in MyController.java

 @Controller
        public class MyController {

            @RequestMapping(value="/my")
            public String getMyHomePage(Model model) {
                LinkedList<String> listOfMyFriends = new LinkedList<String>();
                listOfMyFriends.add("friend1");
                listOfMyFriends.add("friend2");
                listOfMyFriends.add("friend3");
                listOfMyFriends.add("friend4");
                model.addAllAttributes(listOfMyFriends);
                return "my";
            }
        }    

Upvotes: 0

Views: 446

Answers (2)

codeMan
codeMan

Reputation: 5758

Replace the line before the return with

model.addAttribute("listOfMyFriends", listOfMyFriends);

When you do a model.addAllAttributes(listOfMyFriends);

It Copies all attributes in the supplied Collection into this Map, using attribute name generation for each element.

Attribute name generation is done using the method : Conventions.getVariableName()

So, if you are not certain what will be the generated attribute name, use

 model.addAttribute("listOfMyFriends", listOfMyFriends);

So that you can access the list of your friends using the key "listOfMyFriends"

Upvotes: 5

Tarun Gupta
Tarun Gupta

Reputation: 1669

Add listOfMyFriends to the Model as follows :

model.addObject("listOfMyFriends ", listOfMyFriends );

Upvotes: 2

Related Questions