java - JSTL中计算的jsp中访问JSTL变量

标签 java jsp jstl el

在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 Action 类,它是 ArrayListInfo

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) 中我不能使用${}

我可以访问 <% %> 中的那些变量吗?或者我可以使用 formatter .format()<c:>标签

最佳答案

使用 JSTL Format Number

<%@ 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 } " />

关于java - JSTL中计算的jsp中访问JSTL变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25591194/

相关文章:

java - 方法参数和 JSTL

java - 如何用jsp和java从其他网站获取内容???

java - 存储在 ModelMap 中的 HTML 标签不会被浏览器解释为 HTML

java - 每个问题的 JSTL

java - 是什么导致了 AWT-EventQueue-0 中的 NullPointerException?

java - FirebaseRecylerOptions 的实现是什么(不是 FirebaseRecylerAdapter)

java - Spring Maven DispatcherServlet noHandlerFound

xml - JSTL x :forEach comma-separated output

java - 在 Wildfly 9 上部署 Spring Boot 应用

java - 使用 borderlayout 将组件添加到框架