?
Solved

Set Default Value in html:optionsCollection

Posted on 2008-11-03
5
Medium Priority
?
5,981 Views
Last Modified: 2013-11-24
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.
                       
0
Comment
Question by:vino09
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:laes_
ID: 22874718
         <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
 
LVL 2

Expert Comment

by:laes_
ID: 22885670
is it OK ?
0
 

Author Comment

by:vino09
ID: 22886048
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
 
LVL 2

Expert Comment

by:laes_
ID: 22886904
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
 

Accepted Solution

by:
vino09 earned 0 total points
ID: 22941976
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
A solution for Fortify Path Manipulation.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question