Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retaining optionsCollection Values

 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am displaying dropdown in my JSP by optionsCollection tag. But when the same JSP is displayed again after submission. optionsCollection is empty (dropdown is empty).

Please Help!

Thnx
 
Jack Wiesenthaler
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what scope are you storing the collection?
 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Request. Only the Dropdown values are not available, rest of the values are available(textbox)
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered placing it in application scope?
 
Pranav Sharma
Ranch Hand
Posts: 258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one way is to make a class with a method that returns a List with a labelvaluebean objects(your options collection)

in your form get a static instance of the class and in your jsp you would
put
CODE] <html:select property="field"
<bean efine id="values" name="Form" property="options.yourMethod"
type="java.util.ArrayList"/>
<html ptions collection="selections" property="select" labelProperty="label"/>
</html:select>[ [/CODE]
 
satish sat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

the options list probably would have become empty,
so u can set the list like

request.setAttribute("nameOfListUsedinJsp" , list);
probably this should work

or else

if u r validating using validation.xml
the list cannot be retrieved once the request is served
so u should get that from an Util class and set that list in the Form
if the list is empty
 
sudhakar Tadepalli
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satish,
I have the same problem but seeting the attribute in jsp did not help, Can you show us a sample to use Util clas to get hat back?

Thanks
Sudhakar
 
sudhakar Tadepalli
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more question: If i want to go next page, how to pass the collection which is inthe formbean wit hrequest scope?

I tried seeting attribute to request object, but i got null object in the action class.

Could some one give a suggestion please?

Thanks
Sudhakar
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, for most dropdown collections it is best to place them in application scope. This is for collections that are the same for every user and rarely (if ever) change.
 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem here is that If u have 50 dropdowns in ur application then you will have to store all of them in application scope. WOW Now, that is too much info in application scope.

I am quite amazed that their is just no better way out.
 
Pranav Sharma
Ranch Hand
Posts: 258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like Marc said put those collections in application scope that are used by most users and will not change.

But if you did want to bring it up again, rebuild it. Because, technically when you hit the submit button you are invoking a new request(so the list is actually retained in request, only you have a different request) and retaining the list means really you would need to retain it in session or application scope. as these are the same when you hit the submit button
[ August 24, 2005: Message edited by: mannu kapoor ]
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sahil Sharma:
Problem here is that If u have 50 dropdowns in ur application then you will have to store all of them in application scope. WOW Now, that is too much info in application scope.

I am quite amazed that their is just no better way out.


In most cases, the performance will be better to use application scope. Alternatively, you are constantly regenerating the collections, possibly each time even making database calls. What's worse, you can end up with duplicates of the dropdown collections in scopes when using request OR session.

Let's say you've that hypothetical 50 dropdowns in an application. I am assuming that you have a large number of users, else memory would be a non-issue. Let's say at any given time there are an average of 50 users placing requests to the system on pages that have a single dropdown. In that case, you are STILL always with 50 dropdowns in memory but now the server's processor is also bogged down with repeatedly generating the collections and garbage collecting. If the generation involves a database call the performance hit is even worse.

Since duplicates can be created when using request or session, it is very possible for a 50 count dropdown app to have MORE than 50 dropdown collection copies fighting for memory at any given time. Imagine a load test that makes 1000 simultaneous requests! Yowzers! But when using application scope, you are certain there are never more than 50 collections, just one copy of each.
 
sudhakar Tadepalli
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put this bean infor in Page-A how to get collection from this bean in Page B ?. Thanks for your help.

<jsp:useBean id="testForm"
class="com.xxx.xxx.web.formBeans.TestForm"
type="com.xxx.xxx.web.formBeans.TestForm" scope="application"></jsp:useBean>

Thanks
Sudhakar
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I should have described how to go about placing the collections in application scope.

You'll usually want to load them in at startup.

But how to load them at startup?
First you need to create a servlet and configure it in the web.xml:

You can also set init-param tags to allow configuration of various application settings. You can look at the Struts ActionServlet's configuration to see how to add init-params. However, unlike the ActionServlet class, you will not give it a servlet-mapping because we only need it to run at startup.

So now your configuration servlet will run when the application starts. You'll want to put a method in your servlet that looks like this:

"ServletContext" IS application scope. I placed a hypothetical collection of states in application scope under the name "states". (I left out the gory details of building the collection.) I also set one for account types. You can set as many of these as needed.

So now in all JSPs that need a dropdown of states, the Struts tags can reference it with name="states" and it should get pulled automagically from application scope.
 
sudhakar Tadepalli
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahaaaa...got it. Thanks Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic