【问题标题】:NumberFormatException ArrayList size Javabean method in JSP with JSTL带有 JSTL 的 JSP 中的 NumberFormatException ArrayList 大小 Javabean 方法
【发布时间】:2012-10-11 22:50:54
【问题描述】:

我对 JSTL 和 Javabeans 有点陌生,所以我很难弄清楚这一点。

我有一个 index.jsp,一个名为 CustomerScheme 的类,它扩展了一个 ArrayList,还有一个 test.jsp,我用于输出。

index.jsp 包含以下代码,并有一个指向 test.jsp 的链接:

<jsp:useBean id="scheme" scope="session" class="customer.beans.CustomerScheme">

    <%
        // Open a stream to the init file
        InputStream stream =
                application.getResourceAsStream("/fm.txt");

        // Get a reference to the scheme bean
        CustomerScheme custScheme =
                (CustomerScheme) pageContext.findAttribute("scheme");

        // Load colors from stream
        try {
            custScheme.load(stream);
        } catch (IOException iox) {
            throw new JspException(iox);
        }
    %>

</jsp:useBean>

test.jsp 包含:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
${scheme.size}

CustomerScheme 扩展了 ArrayList,并有方法:

public int getSize() {
    return this.size();
}

CustomerScheme 有更多代码。如果你需要,我会发布它。

我的问题是这样的:每次运行程序,我都是从index.jsp开始,点击链接进入test.jsp,然后得到如下:

【问题讨论】:

  • EL 将对象视为map(强制因子)无法解析size 字段。所以需要调用getSize()方法——${scheme.getSize()}或者使用&lt;jsp:getProperty /&gt;
  • 嗯,好的。出于某种原因,我被引导认为你可以访问这样的属性:${guy.name.firstName},如果 guy 有一个函数 getName(),并且这个名字有一个函数 getFirstName()。有什么类似的东西我可能会混淆吗? --顺便说一句,您的评论解决了我的问题,如果您将其作为答案提交,我会给您正确的答案。

标签: jsp jstl javabeans numberformatexception


【解决方案1】:

javax.el.ListELResolver 处理 java.util.List 类型的基础对象。它接受任何对象作为属性并强制将该对象转换为列表中的整数索引。

所以你需要调用getSize()方法——${scheme.getSize()}或者使用&lt;jsp:getProperty/&gt;

或者,您可以在customer.beans.CustomerScheme 中创建List&lt;T&gt;,而不是扩展ArrayList&lt;T&gt;

public class CustomerScheme{
  private List<Customer> list=new ArrayList<Customer>();
  public int getSize(){
         return list.size();
  }
  ..
}

【讨论】:

    【解决方案2】:

    如果您不想创建额外的包装类,您可以执行以下操作:

    <c:set var="size"><jsp:getProperty name="scheme" property="size"/></c:set>
    size : <c:out value="${size}"/>
    

    【讨论】:

      猜你喜欢
      • 2012-01-09
      • 1970-01-01
      • 2016-06-07
      • 2014-07-06
      • 1970-01-01
      • 2020-09-26
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多