aspose file tools*
The moose likes Struts and the fly likes html:options usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html:options usage" Watch "html:options usage" New topic
Author

html:options usage

Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
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
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Question 1:

That's how I'd do it.

Question 2:

<htmlptions name="mylist"/>

Where myList is your ArrayList you put in the request.
[ May 18, 2006: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
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
Vivek Jaiswal
Greenhorn

Joined: May 20, 2006
Posts: 9
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.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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
Consultant, Sima Solutions
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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

Joined: May 20, 2006
Posts: 9
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

Joined: May 03, 2006
Posts: 41
a good article:

http://www.learntechnology.net/validate-manually.do
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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" >
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: html:options usage