有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在会话中放置列表并在Javascript中使用它

我有一个从数据库中提取的通知消息列表,并将其放入servlet中的会话中。然后使用requesDipstacher.forward(request,response将其转发到jsp页面。现在,我获取消息列表并使用jstl循环它:

<c:foreach value = "item" items = '${sessionScope.notification}'>
   <a href = "javascript:printAll('${item}'}>Print</a>
</c:foreach>

javascript如下所示

function printAll(item)
{  
  display(item) //note this is pseudo code . Here I kind of display the items.
}

现在我的问题是在我的网页中显示print的链接太多了。我想将整个列表发送到javascript中,并对其进行迭代,然后显示各个消息。这在javascript中是可能的。请给我指出一个相同的问题,如果它存在的话


共 (1) 个答案

  1. # 1 楼答案

    如果将javascript放在页面的同一个文件中没有问题,可以这样放置:

    <script>
    function printAll()
    {
      var html = '<ul>';
      <c:foreach value = "item" items = '${sessionScope.notification}'>
        html += '<li>${item}</li>';
      </c:foreach>
      html += '</ul>';
      display(html);
    }
    </script>
    
    <a href = "javascript:printAll();">Print</a>
    

    正如你所注意到的,有各种各样的方法来解决这个问题