【发布时间】: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()}或者使用<jsp:getProperty /> -
嗯,好的。出于某种原因,我被引导认为你可以访问这样的属性:${guy.name.firstName},如果 guy 有一个函数 getName(),并且这个名字有一个函数 getFirstName()。有什么类似的东西我可能会混淆吗? --顺便说一句,您的评论解决了我的问题,如果您将其作为答案提交,我会给您正确的答案。
标签: jsp jstl javabeans numberformatexception