如果field是另一个类,则在jstl中访问类的字段

时间:2012-08-07 13:21:17

标签: java jsp jstl

我有一个bean类:

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>

谢谢

stacktrace:

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)

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

<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>

请注意,我将list重命名为element,因为该变量是Class2的实例,而不是列表。另请注意,与Java(它会导致NullPointerException)不同,如果链中的一个bean为空,EL将自动将表达式计算为null