<c:foreach> jstl 标签不打印任何内容



标签不工作…Jsp不打印任何东西…我可以在Eclipse中调试jsp页面吗?查看

<%@ 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 .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";
            }
        }    

return前一行替换为

model.addAttribute("listOfMyFriends", listOfMyFriends);

当你做一个model.addAllAttributes(listOfMyFriends);

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

属性名的生成使用方法:Conventions.getVariableName()

因此,如果您不确定generated attribute name将是什么,请使用

 model.addAttribute("listOfMyFriends", listOfMyFriends);

这样你就可以使用键"listOfMyFriends"

访问你的朋友列表

将listOfMyFriends添加到模型中,如下所示:

model.addObject("listOfMyFriends ", listOfMyFriends );

最新更新