简体   繁体   English

在jsp中显示数组值

[英]Displaying array values in jsp

I have following two array in my code 我的代码中有两个数组

List<Double> centralityList = (List<Double>) request
            .getAttribute("centralityList");

List<String> labelList = (List<String>) request
            .getAttribute("labelList");.

Now I have six string values and corresponding 6 double values of the string in these two array. 现在我在这两个数组中有六个字符串值和相应的6个字符串双精度值。 My question is how to display them in tabular format in my JSP?Is this possible 我的问题是如何在我的JSP中以表格格式显示它们?这是可能的吗?

For Example: 例如:

label list contains [a,b,c,d,e] 标签列表包含[a,b,c,d,e]
centrality list contains- [4,6,8,9,0] 中心列表包含 - [4,6,8,9,0]

So now I want to display output like: 所以现在我想显示如下输出:

label list   centrality list

  a                 4
  b                 6
  c                 8.
  .                 .

etc 等等

Yes of course you can. 是的,你当然可以。 You can use scriplets but they are not recommended. 您可以使用scriplets但不建议使用它们。 Instead use JSTL. 而是使用JSTL。

Try this out: 试试这个:

Have this at top of your JSP: 将它放在JSP的顶部:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 

And code for displaying data 和用于显示数据的代码

<c:forEach begin="0" end="${fn:length(centralityList) - 1}" var="index">
   <tr>
      <td><c:out value="${centralityList[index]}"/></td>
      <td><c:out value="${labelList[index]}"/></td>
   </tr>
</c:forEach>

try this code 试试这段代码

    <%
   List<Double> centralityList = (List<Double>) request
            .getAttribute("centralityList");

   List<String> labelList = (List<String>) request
            .getAttribute("labelList");

    String myString="";
%>
<table>
<tr><td>
<%

    for(int i = 0; i < labelList.size(); i++)
    {
       out.println((String)labelList.get(i));
    }

    %>
</td><td>
<%

    for(int i = 0; i < centralityList.size(); i++)
    {
       out.println((double)centralityList.get(i));
    }

    %>
</td>
</table>

You can achieve this eaisly by using JSTL which is even more easy and far better way but as in your code I didn't find any evidence of using JSTL , so this is the way for now 你可以通过使用JSTL来实现这一点,这是更简单和更好的方法,但在你的代码中我没有找到使用JSTL的任何证据,所以这是现在的方式

You can use JSTL tags and iterate through this- 你可以使用JSTL标签并迭代这个 -

<c:forEach var="Array" items="userNames">

         // do something with the element
</c:forEach>

Use this in your jsp page 在jsp页面中使用它

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM