如何在 JSP 中创建对象并使用 EL/JSTL 访问它



我在 JSP 文件中创建了一个对象,如下所示

<%
Client c1= new Client(dbc,id);
pageContext.setAttribute("c1", c1 );
%>

稍后我需要在我的标签中访问它,如下所示

<t:client_layout title="">
<jsp:attribute name="content">
.....
${pageScope.c1.getFirstName()}//working
${pageScope.c1.sa.getBalance()}//not working!
.....
</jsp:attribute>
</t:client_layout>

奇怪的部分是我无法访问客户端中的对象,这是c1.sa

请注意,c1.sa.getBalance()在我的 eclipse 测试驱动程序中工作

我目前正在使用如下所示的丑陋解决方案

Client c1= new Client(dbc,id);
pageContext.setAttribute("c1", c1 );
pageContext.setAttribute("sa", c1.sa );
pageContext.setAttribute("at", c1.sa.at );

并使用访问它

${pageScope.at.getName()}
${pageScope.sa.getBalance()}

如果有更好的方法可以做到这一点,请告知。

最新更新