Reputation: 810
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>
Upvotes: 0
Views: 753
Reputation: 1108702
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;
}
// ...
}
Upvotes: 1
Reputation: 3456
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.
Upvotes: 0