• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

html:options usage

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys.

I have an ArrayList of data in my action and I want to put it into the request scope so that the JSP I forward to can use the ArrayList to populate an HTML select box.

Question 1:
In my action, should I use something other then:
request.setAttribute("options", optionArrayList);
Not sure if there's a more "struts" way to do this.

Question 2:
In my JSP, how do I use "<html ptions>"? If I understand it correctly, this tag saves me from having to write a scriptlet to loop over the ArrayList myself.

thanks,

jason berk
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 1:

That's how I'd do it.

Question 2:

<html:options name="mylist"/>

Where myList is your ArrayList you put in the request.
[ May 18, 2006: Message edited by: Gregg Bolinger ]
 
Jason Berk
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gregg -

thanks for the reply. Here's my next issue.

how can I get the array lists back into the request scope when validation fails.

I have a page with a text box and a select box. Text box is requires. If a user submits the form without a text box entry, the required rule trips. Because of the way I have my JSPs behind web-inf and am using modules, my action's input param MUST be a forward. (inputForward="true" in the controller settings). When the JSP is redrawn, my array lists are no longer in the request and I'm getting a "Cannot find bean under name..." error.

I'm trying to avoid putting them into the request scope if possible.

thanks,

Jason
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
m using this code to populate my drop down:-

<logic resent name="Wards" scope="request" >
<bean efine id="optionList" name="Wards" scope="request" type="java.util.Collection"/>
<html:select property="ward" >
<html ption value="select">--Select--</html ption>
<html ptions collection="optionList" property="label" labelProperty="value" />
</html:select>
</logic resent>

the thing is working fine...m getting all the values in the drop down. But when the control comes from the action class to the jsp i want one of the options to be automatically selected based on the link that called the action class.....
please suggest how do i achieve this.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brent,

When something is in request scope, it's gone forever once the page is sent to the browser.

You have 3 choices:

1. Bite the bullet and put the ArrayList in session scope. This is the easiest way, and if you clean up after yourself when you're done, the effect on performance is minimal.

2. Override the reset() method of your ActionForm bean and put in code to recreate the ArrayList and put it in request scope. Struts calls the reset() method before performing validation, so if validation fails, the ArrayList will be there when the page is displayed.

3. If the ArrayList contains data that is the same for every user (e.g. States, colors, etc.) Put it in Application Scope. That way there's only one copy of it in memory and everyone uses it.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,

Just pass a value for the property "ward" as a parameter in the link like this:

<a href="myAction.do?ward=xyz">click here</a>

Struts will then populate the ward property with the value xyz, and as long as one of your options in the collection is "xyz", that value will show as being selected by default when the page is displayed.
 
Vivek Jaiswal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Merrill Higginson:
Vivek,

Just pass a value for the property "ward" as a parameter in the link like this:

<a href="myAction.do?ward=xyz">click here</a>

Struts will then populate the ward property with the value xyz, and as long as one of your options in the collection is "xyz", that value will show as being selected by default when the page is displayed.




thanks for the idea merril....but sorry to say that it didnt work....
c the thing is that after i click on the link the control is transfered to the action class which gets some values from the services n then returs me to some other jsp which has got its own form bean and action class...now help me how do i retain the value of 'ward' and let it be selected by default in the drop down of the new jsp?
i thought of using a hidden variable in the jsp with the same name as 'ward', but got lost in the idea....please help!
 
Jason Berk
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a good article:

http://www.learntechnology.net/validate-manually.do
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method I gave you will work as long as the ActionForm used by the action called from the link is the same as the ActionForm used by the JSP that gets displayed. If that is not the case, all you have to do is get the value from the request as a parameter and then populate the value of ActionForm used by the JSP. Something like this:

String ward = request.getParameter("ward");
myForm.setWard(ward);

You would still call the action the same way:

<a href="myAction.do?ward=xyz" >
reply
    Bookmark Topic Watch Topic
  • New Topic