Link to home
Start Free TrialLog in
Avatar of ayeen
ayeenFlag for United States of America

asked on

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

Avatar of boonleng
boonleng
Flag of Malaysia image

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>
Avatar of ayeen

ASKER

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>

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

private String selecteddriver = null;

Change it to:

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

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

Did you try using LabelValueBean?
Avatar of ayeen

ASKER

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?




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>
++++++++++++++++++++
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>
Avatar of ayeen

ASKER

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
Avatar of ayeen

ASKER

raj3060:
how do i use the LabelValue Bean?
Where is you form submitted to? own action?
can you post the struts-config.xml.
Avatar of ayeen

ASKER

-----------------------------
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>
ASKER CERTIFIED SOLUTION
Avatar of boonleng
boonleng
Flag of Malaysia 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
Avatar of ayeen

ASKER

geez! i can't believe it was just that simple *LOL*..thanks a lot boonleng!