【问题标题】:JSTL getting values from Object arrayJSTL 从对象数组中获取值
【发布时间】:2013-04-05 13:21:27
【问题描述】:

我的动作类有一个像这样的对象数组,

Object[] varCount = (Object[]) countList.get(0); 

我的调试显示了 varCount 的值。我把这个对象数组放在模型中如下:

model.put("varCount ", varCount );

在 JSP 中我迭代如下:

<c:forEach var="varCount " items="${model.varCount }" varStatus="loop">
     <tr>
     <td align="center">&nbsp;<c:out value="${varCount[0]}"/></td>
</tr>
     </c:forEach>

我得到以下错误:

Wrapped exception:
javax.servlet.jsp.JspException: An error occurred while evaluating custom action attribute "value" with value "${varCount [0]}": Unable to find a value for "0" in object of class "java.math.BigDecimal" using operator "[]" (null)
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:306)
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:16

如何获取值?

【问题讨论】:

    标签: java jsp struts jstl


    【解决方案1】:

    这样使用获取所有数组对象

    <c:forEach var="item" items="${model.varCount }" varStatus="loop">
     <tr>
     <td align="center">&nbsp;<c:out value="${item}"/></td>
      </tr>
     </c:forEach>
    

    【讨论】:

    • 我必须得到从 0 到 10 的所有元素。
    • 当您遍历 Object 数组时,每次迭代都会从数组中获取一个对象。我假设每个对象都是这个数组中的 BigDecimal。
    • 只是说,您使用了 ,这没有意义,因为 ${varCount} 本身就是您在迭代中的对象.
    【解决方案2】:

    model.varCount 是一个数组或对象,包含 BigDecimal 实例。

    forEach 循环遍历该数组的所有元素。在每次迭代中,当前元素都存储在 varCount 页面属性中。当前元素是BigDecimal 的一个实例。 varCount[0] 因此没有意义。

    【讨论】:

      猜你喜欢
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 2016-12-24
      • 2017-10-06
      • 2016-09-06
      相关资源
      最近更新 更多