简体   繁体   English

在JSTL中的计算jsp中访问JSTL变量

[英]Accessing JSTL variable in jsp of calculation in JSTL

In jsp i have this portion and works fine 在jsp中我有这个部分并且可以正常工作

<c:forEach var="info" items="${infoList}" >
<tr>
    <td>${info.key} </td>
    <td>${info.total}</td>
    <td>${info.delay}</td>
</tr>
</c:forEach>

here infoList comes from struts action class which is a ArrayList of Class Info 这里infoList来自struts动作类,它是类InfoArrayList

public class Info{
private String key;
private String total;
private String delay

}

Now i need to add another <td> which will print the percentage calculated like ${info.delay}*100/${info.total} but will print exactly two digit after decimal. 现在,我需要添加另一个<td> ,它将打印计算出的百分比,如$ {info.delay} * 100 / $ {info.total},但将精确显示小数点后两位。

I tried this 我试过了

${ info.totalDelay*100/info.totalShipment } 

which print the percentage but can't limit to 2 digit. 它打印百分比,但不能限制为2位数字。 In java i can use 在java中我可以使用

NumberFormat formatter = new DecimalFormat("#0.00");

But how i will do that i here because in in <% %> (jap tag) i cant use ${} 但是我在这里会怎么做,因为在<% %> (jap tag)我不能使用${}

Can i access those variable in <% %> some how Or can i use formatter .format() in a <c:> tag 我可以在<% %>访问这些变量的一些方法还是可以在<c:>标记中使用formatter .format()

Using JSTL Format Number 使用JSTL 格式编号

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatNumber type="number" minFractionDigits="2" maxFractionDigits="2" value="${ info.totalDelay*100/info.totalShipment } " />

Well i figured it out 好吧,我想通了

in the top i did this : 在顶部,我这样做:

<%
 NumberFormat numberFormat=new DecimalFormat("#.##");
%>

And i the td i did this: 我是我这样做的:

<td>
<c:set var="tl" value="${info.total}"></c:set>
<c:set var="dl" value="${info.delay}"></c:set>
<jsp:useBean id="tl" class="java.lang.String"/> 
<jsp:useBean id="dl" class="java.lang.String"/> 

<%
     out.println(numberFormat.format(Double.parseDouble(dl)*100/Integer.parseInt(tl)));
%>
</td>

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

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