Link to home
Start Free TrialLog in
Avatar of vino09
vino09Flag for United States of America

asked on

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.
                       
Avatar of laes_
laes_

         <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;
      }

is it OK ?
Avatar of vino09

ASKER

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  
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

ASKER CERTIFIED SOLUTION
Avatar of vino09
vino09
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial