Why aren't you using the struts taglib framework to do this since the bean
is in the request scope, it makes it a lot easier and keeps your JSP code
very clean. And to do what I suspect you may be trying to do with the "odd"
variable, you could also use the JSTL core taglib like follows:
<c:forEach id="threadBean" items="${ThreadBeans}" varStatus="idx">
<c:choose>
<c:when test="${(idx.count+1)%2==0}">
<%-- This is odd logic --%>
</c:when>
<c:otherwise>
<%-- This is your even logic --%>
</c:choose>
<c:out value="${threadBean.sender}" />
...
</c:forEach>
See how clean this looks?
You can also do something simliar with the <logic:iterate> tag as a
replacement for the c:forEach core tag; however I'm partial to the core JSTL
library as its very flexible. ;-)
Unless you can show argument why using a "scriplet" is necessary, I believe
most will continue to suggest the above approach as it's a more efficient
approach.
Take care,
Chris
-----Original Message-----
From: Caroline Jen [mailto:[EMAIL PROTECTED]
Sent: Sunday, June 13, 2004 1:19 PM
To: Struts Users Mailing List
Subject: Re: Cannot Find Bean in Any Scope
Sorry, I still did not explain my code very well.
I am trying to write out the properties of ThreadBean
in a "while loop" in my JSP:
<%
Collection threadRows = ( Collection
)request.getAttribute( "ThreadBeans" );
int odd = 0;
Iterator iterator = threadRows.iterator();
while( iterator.hasNext() )
{
odd = ( odd + 1 )%2;
ThreadBean threadBean = ( ThreadBean
)iterator.next();
.....
.....
<%=threadBean.getSender()%> >
<%=threadBean.getThreadReplyCount()%> >
<%=threadBean.getThreadViewCount()%>
}
%>
and the error is that cannot find threadBean in any
scope.
ThreadBeans is a collection, which is created in a
java class that extends Action. And ThreadBeans are
passed in a request scope into my JSP.
ThreadBeans, which is a collection of JavaBean(s).
Each of those JavaBean(s) is a ThreadBean with many
properties to be written out in a loop. The
ThreadBean is in a package.
In my JSP scriplet, I retrieve the collection of those
JavaBean(s) from the request scope.
Then, I enter the while loop to write out the
properties of each of those JavaBean(s).
What should I do so that my JSP knows threadBean is in
the request scope?
Thank you.
-Caroline
--- Christopher Schultz
<[EMAIL PROTECTED]> wrote:
> Caroline,
>
> Wait a second...
>
> > Collection threadRows = ( Collection
> > )request.getAttribute( "ThreadBeans" );
> >
> > int odd = 0;
> > Iterator iterator = threadRows.iterator();
> > while( iterator.hasNext() )
> > {
> > odd = ( odd + 1 )%2;
> > ThreadBean threadBean = ( ThreadBean )iterator.next();
> >
> > .....
> > }
> > %>
> >
> > 5. when I tried to write out the properties; for
> > example:
> >
> > <%=threadBean.getSender()%> <%=threadBean.getThreadReplyCount()%>
> > <%=threadBean.getThreadViewCount()%>
>
> This message doesn't look like it's coming from this
> body of code. Are
> you mixing scriptlets (as above) with 'bean' tags?
>
> If so, you're mixing apples and oranges, since the
> bean tag doesn't do
> anything with "local" variables created by scriptlet
> code.
>
> Are you sure you don't have something like this
> anywhere:
>
> <bean:write name="threadBean" property="name" />
>
> If so, you'll have to put your object into some
> scope (page or request,
> maybe) before using it with the bean tags. That's a
> bit sloppy, though,
> and I'll recommend again that you stick to using the
> taglibs exclusively
> rather than scriptlets.
>
> -chris
>
> ATTACHMENT part 2 application/pgp-signature
name=signature.asc
__________________________________
Do you Yahoo!?
Friends. Fun. Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]