0

我有一个豆类:

public class Event{
    private Customer customer
    private String stringdate

    public Event(){}

    /*all getters and setters here*/
}

public class Customer{
    private int custId
    private String custname

    public Customer(){}

    /*all getters and setters here*/
}

有没有办法使用 JSTL 在 jsp 页面上显示内容?

编辑: eventsView 是从 servlet 传入的事件列表。当它是一个 int 字段时,我可以显示“cust”变量,但是当我将它更改为自定义类 Customer 时,我无法显示它,因为我得到下面的异常。

<c:forEach items="${ eventsView }" var="events" varStatus="index">
                    <tr>
                        <td>${ index.count }</td>
                        <td>${ events.customer.custid }</td>
                        <td>${ events.stringdate }</td>

                    </tr>
</c:forEach>

谢谢

堆栈跟踪:

SEVERE: Servlet.service() for servlet [jsp] in context with path [/CaspinolCustTest] threw exception [An exception occurred processing JSP page /event.jsp at line 32

29:         <c:forEach items="${ eventsView }" var="events" varStatus="index">
30:                             <tr>
31:                         <td>${ index.count }</td>
32:                         <td>${ events.customer.custId }</td>
33:                         <td>${ events.stringdate }</td>   

Stacktrace:] with root cause
java.lang.NumberFormatException: For input string: "customer"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:514)
    at javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
    at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
    at org.apache.jsp.event_jsp._jspx_meth_c_005fforEach_005f0(event_jsp.java:148)
    at org.apache.jsp.event_jsp._jspService(event_jsp.java:100)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:679)
4

2 回答 2

2

JSTL 使用公共 getter 方法。您需要公共方法:Event 类中的 getCustomer()、getStringDate() 和 Customer 类中的 getCustId()。

于 2012-08-07T14:51:53.170 回答
1
<c:forEach items="${ class2List }" var="element" varStatus="index">
    <tr>
        <td>${ index.count }</td>
        <td>${ element.test1.foo }</td> <%-- calls element.getTest1().getFoo() --%>
        <td>${ element.test2.bar.baz }</td> <%-- calls element.getTest2().getBar().getBaz() --%>
    </tr>
</c:forEach>

请注意,我重命名listelement,因为该变量是 Class2 的实例,而不是列表。另请注意,与 Java 中不同(会导致 NullPointerException),EL 将自动评估表达式,就null好像链中的 bean 之一为空一样。

于 2012-08-07T13:35:35.073 回答