Set Default Value in html:optionsCollection

Hello,

I'm using LabelValueBean Struts Util class to set drop down list (HTML:SELECT) values in my JSP. I reterive customer state code from the database, while rendering the JSP , I need to set this customer state as a default value in the dropdown list box. Please advice me on how to achive this.

Action Code: Reterives all the states from DB
                    Collection options = new ArrayList();
                                                           
                    while (cursor.next())
                    {
                        String value = cursor.getString(cursor.getMetaData().getColumnName(1).toString());
                        String label = cursor.getString(cursor.getMetaData().getColumnName(2).toString());
                       
                        LabelValueBean option = new LabelValueBean(value, label);
                       
                        options.add(option);  

In JSP
<html:select property="attnCustState"  >
        <html:optionsCollection name="listOfValuesState"  />
</html:select>
If the customer has AZ as his state setup in DB, while reteriving the data, the list box must point to this state.

Thanks for your help in advance.
                       
vino09Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

laes_Commented:
         <html:select<html:optionsCollection name="form1"
          property="List1" value="col1" label="col2" />
        </html:select>
a assume that u define an actionform form1
in witch u define an arraylist List1 with getter and setter
in the getter method of the arraylist : u do like this, sure its not recommended , but its a solution
      public java.util.List<classe1> getList1() {
            if (List1.isEmpty()) {
                  List1.add(new classe1(id, label));
                  List1.add(new classe1(id,label));
                                                                 ..... etc
//or u call the code u put in the action , to get the different options
            }
            return List1;
      }

0
laes_Commented:
is it OK ?
0
vino09Author Commented:
Thanks  Laes, for your possible solution.  I could not implement this solution. Is there an alternate approch to create a dropdown list topoint to a  default code while rendering the JSP.

Thanks,
Dinesh  
0
laes_Commented:
if u want to preserve the mvc pattern, struts form associated with an actionForm
that is the solution
unless you want to code it with jsp tag <%..., the worst thing !
personally, a developped it as mentioned

0
vino09Author Commented:
Hi,

This was the solution that I followed to getby this issue,
In action,
                     while (rs2.next())
                     {
                        String value = rs2.getString(rs2.getMetaData().getColumnName(1).toString());
                        LabelValueBean option = new LabelValueBean(value, value);
                        options.add(option);  
                     }
                     setStatesList(options);   //list attributed defined in the form

In JSP,
<td class="ColumnCellDataL" >
               <html:select name="ReportForm" property="attnCustState">  //attnCustState contains the default state
                    <html:option value="attnCustState"><bean:message   key="customer.address.state.label"/></html:option>
                    <html:optionsCollection name="CompReportForm" property="statesList"  />
                </html:select>      
        </td>

Thanks,
Dinesh
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.