*
The moose likes Struts and the fly likes default value in select tag - - optionsCollection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "default value in select tag - - optionsCollection" Watch "default value in select tag - - optionsCollection" New topic
Author

default value in select tag - - optionsCollection

AnilPrakash Raju
Ranch Hand

Joined: Jul 26, 2004
Posts: 137
Hi,

I was wondering how to make a value in a <select> in dafault when i enter in a jsp page.

eg.<html:select property="periodEnd">
<html ptionsCollection name="statSearch" property="misReportPeriodVec" value="periodEnd" label="periodEnd"/>
</html:select>

How can i make a particular value of periodEnd to be default?

thanks

Anil
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Try this:

in your formbean you have a data-member periodEnd where the value will be put in that is selected in the combobox when user submit form.

private [datatype] periodEnd = value_from_optionCollection; (use the value not the label)

and then it should work.
Good luck!


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
AnilPrakash Raju
Ranch Hand

Joined: Jul 26, 2004
Posts: 137
Hi Roel,

Thanks for you suggestion, but i was more interested when the user came to the jsp, i want the a certain value of periodEnd to be selected(or defaulted) and then the user can select other values.

Thanks.

Anil
Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134
Hi Anil,

You have a property in the ActionForm corressponding to the dropdown name, before coming to the jsp , you set this property value to whatever has to be selected.


"Failure is not when you fall down; its only when you fail to get up again."
AnilPrakash Raju
Ranch Hand

Joined: Jul 26, 2004
Posts: 137
Hi Shankar,
I tried it but it does not seem to work.

Do you think i need to change the order of the values of the vector so that the default value is at the top of the list?

Thanks

Anil
Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134
work out this problem with the answer I gave to your other post.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Originally posted by AnilPrakash Raju:
I tried it but it does not seem to work.


Post the code that you tried and think should work. The suggested solutions by the two posters above are correct.


Junilu - [How to Ask Questions] [How to Answer Questions]
AnilPrakash Raju
Ranch Hand

Joined: Jul 26, 2004
Posts: 137
Hi,

in the action just before going to the jsp, i set the field in the actionForm.

code:

statSearchForm.setPeriodEndingString(statSearch.getPeriodEndingString());

the bean statSearch also contains a vector having the values of the <select> tag.


in the jsp :
<html:select property="periodEndingString">
<html ptions name="statSearch" property="periodEndVec"/>
</html:select>

thanks
Anil
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

I guess that you make the change to statSearchForm, but that another instance is created when the jsp-page is called.

change into your struts-config.xml the action that corresponds with your statSearchForm the attribute scope (default: request) into session. so you've got something like this:



another possibility is initialising your periodEndingString into your StatSearchForm, something like this


So in each instance made of this form your data member periodEndingString will be initialized with "blahblahblah"
[ August 09, 2004: Message edited by: Roel De Nijs ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Originally posted by AnilPrakash Raju:

statSearchForm.setPeriodEndingString(statSearch.getPeriodEndingString());


Check these:

1. Is statSearch.getPeriodEndingString() returning a valid option value?

2. Are the options successfully populated in the response?
AnilPrakash Raju
Ranch Hand

Joined: Jul 26, 2004
Posts: 137
Hi,

To your ques1- statSearch.getPeriodEndingString() is returning a valid option value.

And to your ques2- The options are successfully populated in the response.

I am able to maintain the selected option once it is selected.

I am no longer using optionsCollection, just options(that should not make a difference i think ??)

Anil
 
Don't get me started about those stupid light bulbs.
 
subject: default value in select tag - - optionsCollection
 
Similar Threads
Drop Down Box in Struts - Select Default Value Implementation
Retaining selected value in html:select on page refresh
html:select default select label and value
help on html:select for default value
disabled property in html:select