【问题标题】:<c:foreach> jstl tag doesn't print anything<c:foreach> jstl 标签不打印任何内容
【发布时间】:2014-03-26 12:29:58
【问题描述】:

标签不起作用... 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>

控制器在 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";
            }
        }    

【问题讨论】:

    标签: java eclipse jsp spring-mvc jstl


    【解决方案1】:

    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"访问您的朋友列表

    【讨论】:

    • @Capril Aprilovich 欢迎您。如果这是您想要的,请接受答案。
    【解决方案2】:

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

    model.addObject("listOfMyFriends ", listOfMyFriends );

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 2019-05-26
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多