• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1669
  • Last Modified:

Storing an attribute in application scope with Struts?

Hi,

I'm trying to store an Enumeration in a Struts action such that its repeatedly available to a JSP (in order to reduce db access).

However, the Enumeration only appears to be available the first time the jsp is loaded. Subsequent use of the jsp results in a drop-down list with no entries.

Here is the code I'm using:

(In the action)
Enumeration estaff = staff.elements();
request.getSession().getServletContext().setAttribute("estaff", estaff);

(In the jsp)
<%
      Enumeration estaff = (Enumeration) getServletContext().getAttribute("estaff");
%>

<html:form action="signin.do" focus="username">
<html:select property="visitingName">
  <html:options collection="estaff" labelProperty="staffName"
property="staffId"/>
</html:select>
</html:form>

Can you tell me why this is happening and how I can resolve it.

Cheers,

Steve
0
stevebeech
Asked:
stevebeech
  • 12
  • 7
  • 6
  • +2
2 Solutions
 
mmuruganandamCommented:
>> request.getSession().getServletContext().setAttribute("estaff", estaff);

Are you sure that "estaff" have value when you put it in the context.


0
 
stevebeechAuthor Commented:
Yes, its an Enumeration created from a Vector (of JavaBeans populated from a db request). I'm sure its there because the first time the jsp is viewed (directly after the Action) there is data in the drop-down box.

The Action which puts the Enumeration in the context is only called once.
0
 
mmuruganandamCommented:
That's great.  Can you just print the enumeration each time you read it from the context.  So that we can either tool into the tag or the object that stored in the context.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
stevebeechAuthor Commented:
Right,

I've replaced the html:select tag with the following:

<%
      while (estaff.hasMoreElements()) {
           out.println(estaff.nextElement());
      }
%>

Again, the enumeration is only printed out on the first view of the jsp. !?!
0
 
mmuruganandamCommented:
Hi, What I am suspecting is that, all your data that goes into the context might not be serialized.  That might be the cause.

Can you serialize the elements that you store in a vector and try again.
0
 
mmuruganandamCommented:
No few servers are not keeping all the session/context data in memory.  They are having its own persistance mechnism.  So all the server vendors are recommending to store the serialized object in the session.

So better serialize your objects and store in the session/context.


Regards,
Muruga
0
 
mmuruganandamCommented:
>>> No few servers

typo, Now few servers ....
0
 
CEHJCommented:
An Enumeration can generally only be used once (i.e. enumerated once). You'd be better using some List implementation
0
 
mmuruganandamCommented:
You are absolutely correct CEHJ.

Hi better store your Vector itself in the context.
0
 
mmuruganandamCommented:
each and every time,

(In the jsp)
<%
     Enumeration estaff = ((Vector) getServletContext().getAttribute("estaff")).elements();
%>


Regards,
Muruga
0
 
mmuruganandamCommented:
(In the action)
request.getSession().getServletContext().setAttribute("estaff", staff);
0
 
mmuruganandamCommented:
You can iterate only once in Enumeration/Iteration.

When you try to iterate for the second time, the control would be at the end of elements.

So hasMoreElements() will return false.

You won't get any elements there after.  

Points to CEHJ


Regards,
Muruga

0
 
CEHJCommented:
Just to make it absolutely clear, 'estaff' is now a Vector [better use 'vstaff' perhaps ;-)]
0
 
stevebeechAuthor Commented:
I was wondering if that was the problem. I've previously used a vectors in the request scope and then created an Enumeration in the jsp page with the following code (well, similar):

<%
while (vc.hasMoreElements()) {
      ClientForm key = (ClientForm) vc.nextElement();
      out.println("<OPTION value='"+key.getClientId()+"'>");
      out.println(key.getCompanyName());
      out.println("</OPTION>");
}
%>

but thought that this was too messy.

Your solution is a lot nicer.

I'll add some more points and award the original 125 to CEHJ, plus another 75 to you, Muruga, for your excellent effort, if thats okay with you both.

Much appreciation,

Steve
0
 
mmuruganandamCommented:
Itz our pleasure...
0
 
Mayank SAssociate Director - Product EngineeringCommented:
BTW, why do you need an enumeration to loop through a vector? You cah directly use:

for ( int i = 0, iSize = vector.size () ; i < iSize ; i ++ )
{
     ClientForm key = (ClientForm) vector.get ( i ) ;
     out.println("<OPTION value='"+key.getClientId()+"'>");
     out.println(key.getCompanyName());
     out.println("</OPTION>");

}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Oops, already closed ;-) sorry for disturbing a PAQed thread.
0
 
CEHJCommented:
8-)
0
 
stevebeechAuthor Commented:
Seems like I accepted Muruga's answer and put CEHJ's as an assisted answer when it should have been the other way round. Does this matter?
0
 
mmuruganandamCommented:
But I got a mail as Good Assist!  I donno....  CEHJ what do you say
0
 
CEHJCommented:
Not really ;-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
It matters for people who would come looking to this page, searching for solutions. If they don't want to read all comments on the page, they would click on the "See Solution" link at the top (near the question) and would be directed to Muruga's comment.
0
 
CEHJCommented:
Everyone gets mail starting 'Good assist..' . Charming of you both to be concerned, but don't worry 8-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> But I got a mail as Good Assist!

Whenever there is a split, everyone gets a mail-alert as "Good Assist" only.
0
 
stevebeechAuthor Commented:
NIce one. Cheers!

Steve
0
 
mmuruganandamCommented:
I am really expecting CEHJ to get the points for finding out a mistake.  
Thanks CEHJ for giving away your points to me.

;)
0
 
CEHJCommented:
>>Thanks CEHJ for giving away your points to me.

I'm not giving them away! I agree you added value to this and you deserve them

>>
they would click on the "See Solution" link at the top (near the question) and would be directed to Muruga's comment.
>>

which is definitely a piece of added value that expands on my terse explanation ;-)
0
 
searlasCommented:
Put the Vector (staff) in the session instead of the Enumeration (estaff)
0
 
CEHJCommented:
?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Perhaps he didn't refresh for an hour ;-)
0
 
searlasCommented:
That'll be it.... just had lunch...
0
 
stevebeechAuthor Commented:
Turns out I don't need an Enumeration at all:

(in the Action)
request.getSession().getServletContext().setAttribute("vstaff", staff);

(in the JSP)
<%
      Vector vstaff = (Vector) getServletContext().getAttribute("vstaff");
%>

(and to create the select box in the JSP):

<html:select property="visitingName">
  <html:options collection="vstaff" labelProperty="staffName"
property="staffId"/>
</html:select>

I hadn't realised the html:options collection attribute could refer directly to a Vector. I think this was what you were making clear CEHJ, but I used Muruga's code with the Enumeration initially.
However, this caused some problems when I tried to detect the presence of the collection with the logic:notPresent tag, due to requiring an initialisation stage first.
So, now I've got the following code before I try to retrieve the Vector from the application scope:

<logic:notPresent name="vstaff" scope="application">
      <logic:redirect href="initialise.do"/>
</logic:notPresent>

That's it. No more comments. Thanks all.
Cheers, Steve   :o)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 12
  • 7
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now