Displaying array values in jsp

后端 未结 3 1428
感动是毒
感动是毒 2021-01-14 17:34

I have following two array in my code

List centralityList = (List) request
            .getAttribute(\"centralityList\");

List&l         


        
相关标签:
3条回答
  • 2021-01-14 17:45

    Yes of course you can. You can use scriplets but they are not recommended. Instead use JSTL.

    Try this out:

    Have this at top of your 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>
    
    0 讨论(0)
  • 2021-01-14 17:54

    You can use JSTL tags and iterate through this-

    <c:forEach var="Array" items="userNames">
    
             // do something with the element
    </c:forEach>
    

    Use this in your jsp page

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
    0 讨论(0)
  • 2021-01-14 18:05

    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

    0 讨论(0)
提交回复
热议问题