Java JSTL forEach 从arrayList 迭代的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2947417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
issue of JSTL forEach iterate from arrayList
提问by Mickey
In my code, I have used ArrayList which stores the number format like '$0.00 to $1,000,000.00' in each index of array list. while iterate in JSP through <c:forEach>
tag,
its values are printing like
在我的代码中,我使用了 ArrayList,它在数组列表的每个索引中存储数字格式,如“$0.00 到 $1,000,000.00”。通过<c:forEach>
标签在 JSP 中迭代时,其值打印如下
$0.00 to $1 as a first string, 000 as a second string and 000.00 as a thrid string. but it has to print like '$0.00 to $1,000,000.00'.
$0.00 到 $1 作为第一个字符串,000 作为第二个字符串,000.00 作为第三个字符串。但它必须打印为“$0.00 to $1,000,000.00”。
what will be the problem is?
问题是什么?
Thanks in advance
提前致谢
采纳答案by Algirdas
You are iterating over element of array, not over array itself. Thus the element of the array "$0.00 to $1,000,000.00" is split at comma positions and you get individual elements as you have described.
您正在迭代数组元素,而不是数组本身。因此,数组“$0.00 到 $1,000,000.00”的元素在逗号位置拆分,您将获得您所描述的单个元素。
Following is an example:
下面是一个例子:
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
java.util.ArrayList list = new java.util.ArrayList();
list.add("<c:forEach var="item" items="list">
<fmt:formatNumber type="currency" currencySymbol="$" value="${item}" currencyCode="USD" />
</c:forEach>
.00 to ,000,000.00");
list.add(",000,000.00 to ,000,000,000.00");
request.setAttribute("list", list);
%>
<h1>Iterating over ArrayList</h1>
<ul>
<c:forEach items="${list}" var="value">
<li><c:out value="${value}"/></li>
</c:forEach>
</ul>
<h1>Iterating over first element of ArrayList</h1>
<ul>
<c:forEach items="${list[0]}" var="value">
<li><c:out value="${value}"/></li>
</c:forEach>
</ul>
回答by hleinone
I'm not sure if I got this right, but consider using the JSTL fmt tag library. Its formatNumber tagcan handle currencies. Your example would then become:
我不确定我是否做对了,但可以考虑使用JSTL fmt 标记库。它的formatNumber 标签可以处理货币。您的示例将变为:
c:forEach items="${list}" var="value" >
回答by gaurav devdutt
hey I had the same problem, the solution is to trim the spaces in the for:each
tag.
嘿我有同样的问题,解决方案是修剪for:each
标签中的空格。
So instead of this (notice the space before the '>' )
所以而不是这个(注意 '>' 之前的空格)
c:forEach items="${list}" var="value">
to
到
##代码##// no spaces.
// 没空间了。
This will work for sure.. Its frustrating... but that's the way it is.
这肯定会奏效……令人沮丧……但事情就是这样。