【问题标题】:jsp JSTI foreach: array from request.setAttributejsp JSTI foreach:来自 request.setAttribute 的数组
【发布时间】:2012-10-25 03:09:53
【问题描述】:

我想知道如何遍历 jsp 页面中的列表数组,其中列表是在自定义 java servlet 中定义的。

这就是我在 servlet 中的内容:

request.setAttribute("TITLE", STRINGVALUE);

我可以用下面一行显示上面的代码片段:

${dashboard}

现在我正在尝试使用 foreach 循环:

<c:forEach var="VARIABLE" items="??????">

问题由两个子问题组成:

  1. 如何在 foreach 语句中使用数组而不使用 scriptlet?
  2. 如何打印数组元素的值?例如,${person.name} 会导致错误,因为程序只能看到一个字符串。

【问题讨论】:

    标签: java jsp servlets foreach jstl


    【解决方案1】:

    基本上,您需要在会话中拥有对象列表。

    例如。

    List <Persons> myList = new ArrayList<Persons>();
    myList.add(p1);
    myList.add(p2);
    session.setAttribute("persons", myList );
    

    然后在你的jsp中你像这样使用它:

    <c:forEach items="${persons}" var="personBean">  
      ${personBean.lastName}, ${personBean.firstName} <br />  
    </c:forEach>  
    

    您还可以在Iterate over elements of List and Map using JSTL <c:forEach> tag 中查看 BalusC 的帖子了解更多信息

    【讨论】:

    • 我会试试这个。非常感谢:)
    猜你喜欢
    • 2021-02-10
    • 2015-04-24
    • 2010-09-18
    • 2015-01-19
    • 2012-03-22
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多