簡體   English   中英

在JSTL中的計算jsp中訪問JSTL變量

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

在jsp中我有這個部分並且可以正常工作

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

這里infoList來自struts動作類,它是類InfoArrayList

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

}

現在,我需要添加另一個<td> ,它將打印計算出的百分比,如$ {info.delay} * 100 / $ {info.total},但將精確顯示小數點后兩位。

我試過了

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

它打印百分比,但不能限制為2位數字。 在java中我可以使用

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

但是我在這里會怎么做,因為在<% %> (jap tag)我不能使用${}

我可以在<% %>訪問這些變量的一些方法還是可以在<c:>標記中使用formatter .format()

使用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 } " />

好吧,我想通了

在頂部,我這樣做:

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

我是我這樣做的:

<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