将列表传递到 JSP

发布于 2024-12-28 20:32:40 字数 500 浏览 1 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铜锣湾横着走 2025-01-04 20:32:40

Scriptlet 和 EL 不共享相同的变量范围。 EL 将变量解析为 PageContextHttpServletRequestHttpSessionServletContext 的属性。 Scriptlet 解析仅在方法或类范围内声明的变量。

在这种特殊情况下,您基本上需要将其设置为请求属性:

<%
  mb = mailingbean.getResultProdInfo();
  request.setAttribute("mb", mb);
%>
<data>
  <item>
    <c:out value="<ColourCd>${mb.apid}</ColourCd>" />
  </item>
</data>

但是,这是没有意义的。以下 JSP bean 声明

<jsp:useBean id="mailingbean" scope="request" class="com.dmd.jpa.Imp.AmpImp" />

将使其可由 ${mailingbean} 使用。因此,您需要做的就是

<data>
  <item>
    <c:out value="<ColourCd>${mailingbean.resultProdInfo.apid}</ColourCd>" />
  </item>
</data>

如果您在 getter 方法中执行数据加载工作,我会将其移至 bean 的构造函数,即此类工作实际所属的地方。

public class AmpImp {

    private List<Amp> resultProdInfo;

    public AmpImp() {
        // Do the business job here.
        resultProdInfo = loadItSomehow();
    }

    public List<Amp> getResultProdInfo() {
        // Do NOT do the business job here. Just return the property.
        return resultProdInfo;
    } 

    // ...
}

另请参阅:

Scriptlets and EL do not share the same variable scope. EL resolves variables as attributes of PageContext, HttpServletRequest, HttpSession and ServletContext. 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:

<%
  mb = mailingbean.getResultProdInfo();
  request.setAttribute("mb", mb);
%>
<data>
  <item>
    <c:out value="<ColourCd>${mb.apid}</ColourCd>" />
  </item>
</data>

However, this makes no sense. The following JSP bean declaration

<jsp:useBean id="mailingbean" scope="request" class="com.dmd.jpa.Imp.AmpImp" />

will make it available by ${mailingbean} already. So all you need to do is

<data>
  <item>
    <c:out value="<ColourCd>${mailingbean.resultProdInfo.apid}</ColourCd>" />
  </item>
</data>

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

public class AmpImp {

    private List<Amp> resultProdInfo;

    public AmpImp() {
        // Do the business job here.
        resultProdInfo = loadItSomehow();
    }

    public List<Amp> getResultProdInfo() {
        // Do NOT do the business job here. Just return the property.
        return resultProdInfo;
    } 

    // ...
}

See also:

疾风者 2025-01-04 20:32:40

如果您确定 bean 已填充,请尝试按如下方式指定完整的 bean:

<c:out value="<ColourCd> ${mailingbean.resultProdInfo.apid} </ColourCd>" />

如果您仅使用 JSTL(并且不再使用 scriptlet),则可以完全删除 jsp:useBean 标记。默认情况下,它将尝试从请求或您指定的任何范围中提取值。

If you are sure the bean is populated, try specifying the full bean as follows:

<c:out value="<ColourCd> ${mailingbean.resultProdInfo.apid} </ColourCd>" />

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文