将列表传递到 JSP
我将 bean 列表传递到 JSP 中,然后我想使用 JSTL 检索一些数据,但没有填充任何内容,只是空标记。 有什么想法吗?
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="mailingbean" scope="request" class="com.dmd.jpa.Imp.AmpImp" />
<jsp:useBean id="mb" class="com.dmd.jpa.entity.Amp" />
<%
mb = mailingbean.getResultProdInfo();
%>
<data>
<item>
<c:out value="<ColourCd> ${mb.apid} </ColourCd>"/>
</item>
</data>
I'm passing list of beans into JSP and then I want to use JSTL to retrieve some data but nothing is populated just empty tag.
Any ideas?
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="mailingbean" scope="request" class="com.dmd.jpa.Imp.AmpImp" />
<jsp:useBean id="mb" class="com.dmd.jpa.entity.Amp" />
<%
mb = mailingbean.getResultProdInfo();
%>
<data>
<item>
<c:out value="<ColourCd> ${mb.apid} </ColourCd>"/>
</item>
</data>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Scriptlet 和 EL 不共享相同的变量范围。 EL 将变量解析为
PageContext
、HttpServletRequest
、HttpSession
和ServletContext
的属性。 Scriptlet 解析仅在方法或类范围内声明的变量。在这种特殊情况下,您基本上需要将其设置为请求属性:
但是,这是没有意义的。以下 JSP bean 声明
将使其可由
${mailingbean}
使用。因此,您需要做的就是如果您在 getter 方法中执行数据加载工作,我会将其移至 bean 的构造函数,即此类工作实际所属的地方。
另请参阅:
Scriptlets and EL do not share the same variable scope. EL resolves variables as attributes of
PageContext
,HttpServletRequest
,HttpSession
andServletContext
. Scriptlets resolves variables which are been declared in the method or class scope only.In this particular case, you basically need to set it as a request attribute:
However, this makes no sense. The following JSP bean declaration
will make it available by
${mailingbean}
already. So all you need to do isIf you were doing the data loading job in the getter method, I'd move that to the constructor of the bean, there where this kind of job actually belongs.
See also:
如果您确定 bean 已填充,请尝试按如下方式指定完整的 bean:
如果您仅使用 JSTL(并且不再使用 scriptlet),则可以完全删除
jsp:useBean
标记。默认情况下,它将尝试从请求或您指定的任何范围中提取值。If you are sure the bean is populated, try specifying the full bean as follows:
If you're going with just JSTL (and moving away from scriptlets), you can remove the
jsp:useBean
tags completely. It will attempt to pull the value out of the request by default, or whatever scope you specify.