how do i set a default value for my pull down menu using struts?

how do i set a default value for my pull down menu using struts?

here's the scenario.
1. i have a jsp page that asks the user to select a driver.
2. user chooses "driver 2" from the pulldown menu and hits submits
3. the screen refreshes,  displays the pulldown menu again plus some data about the chosen "driver 2". but what i was hoping to happen is that once the screen refreshes the default value (or highlighted) for my pulldown will be "driver 2" instead of the first item in my pulldown ("driver 1").

notes:
1. my action's scope is session
2. i have the reset() in my form bean but it's empty.


-----------------------------
here's my jsp code:
-----------------------------
<html:select property="selecteddriver" size="1" value="selecteddriver">
     <logic:iterate name="myformbean" property="driverlist" id="ayeen">    
            <option value="<bean:write name="ayeen" property="driverid"/>">
                      <bean:write name="ayeen" property="drivername"/>
           </option>
     </logic:iterate>
</html:select>


-----------------------------
my Form Bean has:
-----------------------------
getSelecteddriver()
setSelecteddriver(String s)
getDriverlist()
setDriverlist(Collection c)



-----------------------------
i have a value object (DriverVO) for the driver and it has:
-----------------------------
getDrivername()
setDrivername()
getDriverid()
setDriverid()



-----------------------------
my action form looks like this:
-----------------------------
MyFormBean mybean = new MyFormBean ();
mybean.setDriverlist(delegate.loadDriverlist());



-----------------------------
my delegate should be calling a dao to dynamically load my DriverVO but im using a static data for now..but it has:
-----------------------------
loadDriverlist() that would return a Collection of my DriverVO value object
snippet from loadDriverlist():
     public Collection loadDriverlist(){
           Collection dlist = new ArrayList();
            dlist.add(new DriverVO("d1" , "driver1"));
            dlist.add(new DriverVO("d2" , "driver2"));
            .
            .          
            return dlist;
    }
   
   
   

-----------------------------
and here is my struts-config...
-----------------------------
<action path="/displaydriverdetails"
type="com.ayeen.actions.MyAction"
name="myformbean"
scope="session">
<forward name="filter" path = "/WEB-INF/pages/my.jsp"/>            
</action>

<form-bean name="myformbean" type="com.ayeen.forms.MyFormBean"></form-bean>




-----------------------------
and here is what my html looks like when i run my application:
-----------------------------
<select name="selecteddriver" size="1">
    <option value="d1 "> driver 1 </option>
    <option value="d2 "> driver 2 </option>
    <option value="d3 "> driver 3 </option>
    <option value="d4 "> driver 4 </option>
</select>

thanks in advance...

LVL 1
ayeenAsked:
Who is Participating?
 
boonlengConnect With a Mentor Commented:
Just remove the attribute "value"' from the select tag will do.

    <html:select property="selecteddriver" size="1">
        <html:optionsCollection name="myformbean" property="driverlist" label="drivername" value="driverid"/>
    </html:select>
0
 
boonlengCommented:
If you want struts to auto default the option value, you need to use struts tag instead of html <option> tag.
However, i don't think struts option tag can handle nested bean at the moment. You need jstl / scriptlet help.
Example using jstl:

      <c:forEach var="item" items="${myformbean.driverlist}">
            <html:option value="${item.driverid}">${item.drivername}</html:option>
      </c:forEach>
0
 
ayeenAuthor Commented:
hi boonleng...thanks for your reply..but i am using a struts tag...

again here's my jsp code:
<html:select property="selecteddriver" size="1" value="selecteddriver">
     <logic:iterate name="myformbean" property="driverlist" id="ayeen">    
            <option value="<bean:write name="ayeen" property="driverid"/>">
                      <bean:write name="ayeen" property="drivername"/>
           </option>
     </logic:iterate>
</html:select>

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
raj3060Commented:
Define the value in your form bean. It will default to the value you define there if one is provided.
0
 
raj3060Commented:
So I am assuming that you have this:

private String selecteddriver = null;

Change it to:

private String selecteddriver = "PUT_YOUR _ DEFAULT_VALUE_HERE";
0
 
raj3060Commented:
Also you should use this:

<html:option value="<bean:write name="ayeen" property="driverid"/>">

Did you try using LabelValueBean?
0
 
ayeenAuthor Commented:
hi raj3060..thanks for the reply...

if i do this:
private String selecteddriver = "PUT_YOUR _ DEFAULT_VALUE_HERE";

that would mean we're manually setting the default value...what i was hoping is the default will depend on whatever the user have chosen before the user hits the submit button... so if user A chose driver 2 when userA hits "submit" the next time userA sees the refreshed screen driver2 will be the default value for the pulldown menu...if userB chooses driver4  and hits "submit" the next time userB sees the refreshed screen driver4 will be the default value for the pulldown menu

i'm not sure i know how to use the LabelValueBean...do i have to completely change my codes?




0
 
raj3060Commented:
This should default to the value that user selected:

<html:option value="<bean:write name="ayeen" property="driverid"/>">

Did you try to change this??

No you don't have to change alot of  the code to use LabelValueBean.

This should work fine too..

++++++++++++++++++++
<html:select property="selecteddriver" size="1" value="selecteddriver">
     <logic:iterate name="myformbean" property="driverlist" id="ayeen">    
            <html:option value="<bean:write name="ayeen" property="driverid"/>">
                      <bean:write name="ayeen" property="drivername"/>
           </html:option>
     </logic:iterate>
</html:select>
++++++++++++++++++++
0
 
boonlengCommented:
Sorry ayeen, my mistake (I kept thinking of nested tags).. you should be able to use struts "optionsCollection" tag to loop the nested beans.
Example:
    <html:select property="selecteddriver" size="1" value="selecteddriver">
        <html:optionsCollection name="myformbean" property="driverlist" label="drivername" value="driverid"/>
    </html:select>
0
 
ayeenAuthor Commented:
hi raj3060:

using <html:option value="<bean:write name="ayeen" property="driverid"/>"> instead of
<option value="<bean:write name="ayeen" property="driverid"/>"> gives me a
Error 500: /WEB-INF/pages/MyJsp.jsp(260,50) Attribute ayeen has no value


hi boonleng:
i have no problem with looping thru my data but you're code looks good too...i tried using your codes but the previously chosen value still doesn't come out as the default value when submit button is clicked
0
 
ayeenAuthor Commented:
raj3060:
how do i use the LabelValue Bean?
0
 
boonlengCommented:
Where is you form submitted to? own action?
can you post the struts-config.xml.
0
 
ayeenAuthor Commented:
-----------------------------
and here is my struts-config...
-----------------------------
<action path="/displaydriverdetails"
type="com.ayeen.actions.MyAction"
name="myformbean"
scope="session">
<forward name="filter" path = "/WEB-INF/pages/my.jsp"/>            
</action>

<form-bean name="myformbean" type="com.ayeen.forms.MyFormBean"></form-bean>
0
 
ayeenAuthor Commented:
geez! i can't believe it was just that simple *LOL*..thanks a lot boonleng!
0
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.

All Courses

From novice to tech pro — start learning today.