[英]How to iterate an object in JSP to get the percentage?
I am working on servlet and jsp project. 我正在研究servlet和jsp项目。 I am passing an object from servlet to JSP. 我正在将一个对象从servlet传递到JSP。 And currently I am iterating that object and showing them in a table - 目前,我正在迭代该对象并将其显示在表格中-
Below is my code in jsp - 以下是我在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. ${reportCount.getDatabaseName().get(i)}
将仅以oracle
或mysql
返回数据库名称。 Now I need to calculate what is the percentage of oracle
database. 现在,我需要计算oracle
数据库的百分比。 I will get the total number of records from ${reportCount.getHash().size()
. 我将从${reportCount.getHash().size()
获得记录总数。 So if total number of records is 10
and oracle database is present 5
times, then the percentage should be 50%
. 因此,如果记录总数为10
而oracle数据库出现5
次,则百分比应为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? 我想我应该在上面的新表中再次迭代reportCount
对象,并在此处提取百分比,但不确定如何做? Can anyone provide an example? 任何人都可以提供示例吗?
UPDATE:- 更新: -
Here is my bean code - 这是我的bean代码-
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: 有以下三件事:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
添加函数taglib: <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<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>
如果在其他任何页面上不需要,则将reportsCounts变量添加到requestScope而不是会话中,并按以下方式使用: <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>
${(oracleDBCount / fn:length(requestScope.reportCounts))*100}%
现在将百分比显示为: ${(oracleDBCount / fn:length(requestScope.reportCounts))*100}%
Based on my experience, doing number-crunching on a JSP page can make it unreadable very quickly. 根据我的经验,在JSP页面上进行数字运算可能会使它很快变得不可读。 Also you may encounter additional requirements in the future such as: 另外,将来您可能还会遇到其他要求,例如:
Is it possible to do the computation inside the servlet while you are making the reportCount object? 在制作reportCount对象时,是否可以在servlet内部进行计算? Then you can pass the value inside a session attribute 然后,您可以在会话属性中传递值
request.getSession(true).setAttribute("oracleReports", "50%")
and then in the JSP output something like 然后在JSP中输出类似
<c:out value="${sessionScope.oracleReports}"/>
Use JavaScript for doing the operation, Here is the code 使用JavaScript进行操作,这是代码
Read the comments to understand the code. 阅读注释以了解代码。
Add an id to the so that i can read the inner content using javascript. 在上添加一个ID,以便我可以使用JavaScript读取内部内容。 Appended id with the index so that each raw gets different id's 在索引后附加id,以便每个原始对象获得不同的id
<c:forEach var="i" begin="0" end="${reportCount.getHash().size() - 1}">
<TD id="databaseName<c:out value='${i}' />">
${reportCount.getDatabaseName().get(i)}
</TD>
Add Id to the so that i can set values to the raw using JavaScript 将ID添加到中,以便我可以使用JavaScript将值设置为原始值
<tr>
<td id="oraclePercentage">%</td>
<td id="mySQLPercentage">%</td>
</tr>
call the javascript function to set the values to the raw, as we don't have any button to trigger we open a raw and add the JavaScript call from the JSP so that the script is triggered every time the page loads the raw. 调用javascript函数将值设置为原始值,因为我们没有任何触发按钮,因此我们打开了原始值并添加了来自JSP的JavaScript调用,以便每次页面加载原始值时都会触发脚本。
<TR><TD><Script>setPercentage('${reportCount.getHash().size() - 1}');</script><TD><TR>
what to do is defined here 这里定义了做什么
<Script>
function setPercentage(var index){
for(var i=0; i<index;i++){
var documentType=document.getElementById("databaseName"+i).innerHTML;
var oracleCount=0;
var mySQLCount=0;
if(vardocumentType=='Oracle')
oracleCount+=(Number)1;
else if(vardocumentType=='MySQL')
mySQLCount+=(Number)1;
document.getElementById("oraclePercentage").innerHTML=(oracleCount/index)*100;
document.getElementById("mySQLPercentage").innerHTML=(mySQLCount/index)*100;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.