本文介绍了如何在JSP中迭代对象以获取百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究servlet和jsp项目。我将一个对象从servlet传递给JSP。目前我正在迭代该对象并将它们显示在表中 -



以下是我在jsp中的代码 -

 < TABLE ID =tableSMSBORDER =1CELLPADDING =3CELLSPACING =1style =text-align:center;> 
< TR style =color:#ffffff; background-color:#787878;>
< TH>哈希名称< / TH>
< TH>数据库名称< / TH>
< TH>版本< / TH>
< / TR>
< TR>
< TD>
$ {reportCount.getHash()。get(i)}
< / TD>
< TD>
$ {reportCount.getDatabaseName()。get(i)}
< / TD>
< TD>
$ {reportCount.getVersion()。get(i)}
< / TD>
< / TR>
< / c:forEach>

上面的代码工作正常,我能够正确显示数据。现在我需要做的是 -



$ {reportCount.getDatabaseName()。get(i)} will仅返回数据库名称 oracle mysql 。现在我需要计算 oracle 数据库的百分比。我将从 $ {reportCount.getHash()。size()>获取记录总数。因此,如果记录总数 10 且oracle数据库存在 5 次,则百分比应该为 50%



现在我不确定如何使用该对象计算上述百分比?在计算这个百分比后,我需要在下面显示的新表中显示结果 -

 < table> 
< tr>
< th style =background-color:#E8E8E6;>< b> Oracle数据库< / b>< / th>
< / tr>
< tr>
< td>%< / td>
< / tr>
< / table>

我想我应该迭代 reportCount 在上面的新表中再次提取对象并提取百分比,但不知道我该怎么做?任何人都可以提供一个例子吗?



更新: -

代码 -

  public class Response {
private List< String> hash = new LinkedList< String>();
私人列表< String> databaseName = new LinkedList< String>();
私人列表< String> version = new LinkedList< String>();

// getters and setters here

}



  1. 添加函数taglib:
    <%@ taglib prefix =fnuri =http://java.sun.com/jsp/jstl/functions%>

  2. 如果在任何其他页面上不需要,则将reportsCounts变量添加到requestScope而不是会话中。使用如下:
    < c:set var =oracleDBCountvalue =0scope =page/>
    < c:forEach var =reportCount
    items =$ {requestScope.reportCounts.databaseName}varStatus =loop>
    < TR>
    < TD> $ {requestScope.reportCounts.hash [loop.index]}< / TD>
    < TD> $ {reportCount}
    < c:if test =$ {reportCount =='ORACLE'}>
    < c:set var =oracleDBCountvalue =$ {oracleDBCount + 1}scope =page/>
    < / c:if>
    < / TD>
    < TD> $ {requestScope.reportCounts.version [loop.index]}< / TD>
    < / TR>
    < / c:forEach>

  3. 现在将百分比显示为:
    $ {(oracleDBCount / fn:length(requestScope.reportCounts) )* 100}%


I am working on servlet and jsp project. I am passing an object from servlet to JSP. And currently I am iterating that object and showing them in a table -

Below is my code in jsp -

<TABLE id="tableSMS" BORDER="1" CELLPADDING="3" CELLSPACING="1" style="text-align: center;">
    <TR style="color:#ffffff;background-color:#787878;">
        <TH>Hash Name</TH>
        <TH>Database Name</TH>
        <TH>Version</TH>
    </TR>
    <c:forEach var="i" begin="0" end="${reportCount.getHash().size() - 1}">
        <TR>
            <TD>
                    ${reportCount.getHash().get(i)}
            </TD>
            <TD>
                    ${reportCount.getDatabaseName().get(i)}
            </TD>
            <TD>
                    ${reportCount.getVersion().get(i)}
            </TD>
        </TR>
    </c:forEach>

And above code is working fine and I am able to show the data properly. Now what I need to do is -

${reportCount.getDatabaseName().get(i)} will return database name as oracle or mysql only. Now I need to calculate what is the percentage of oracle database. I will get the total number of records from ${reportCount.getHash().size(). So if total number of records is 10 and oracle database is present 5 times, then the percentage should be 50%.

Now I am not sure how would I calculate the above percentage using that object? And after calculating that percentage, I need to show the result in a new table which is shown below -

      <table>
          <tr>
          <th style="background-color: #E8E8E6;"><b>Oracle Database</b></th>
          </tr>
          <tr>
              <!-- I would like to show the percentage in this row -->
              <td>%</td>
          </tr>
      </table>

I am thinking I should iterate the reportCount object again in the above new table and extract the percentage here but not sure how would I do that? Can anyone provide an example?

UPDATE:-

Here is my bean code -

public class Response {
    private List<String> hash = new LinkedList<String>();
    private List<String> databaseName = new LinkedList<String>();
    private List<String> version = new LinkedList<String>();

    // getters and setters here

}
解决方案

There are three things as following:

  1. Add the functions taglib:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
  2. Add reportsCounts variable to requestScope instead of session if not required on any other page & use as following:<c:set var="oracleDBCount" value="0" scope="page" /><c:forEach var="reportCount" items="${requestScope.reportCounts.databaseName}" varStatus="loop"> <TR> <TD>${requestScope.reportCounts.hash[loop.index]}</TD> <TD>${reportCount} <c:if test="${reportCount == 'ORACLE'}"> <c:set var="oracleDBCount" value="${oracleDBCount + 1}" scope="page"/> </c:if> </TD> <TD>${requestScope.reportCounts.version[loop.index]}</TD> </TR> </c:forEach>
  3. Now display percentage as:${(oracleDBCount / fn:length(requestScope.reportCounts))*100}%

这篇关于如何在JSP中迭代对象以获取百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:51