Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-08-08
14
Medium Priority
?
2,148 Views
Last Modified: 2013-11-24
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...

0
Comment
Question by:ayeen
  • 6
  • 4
  • 4
14 Comments
 
LVL 14

Expert Comment

by:boonleng
ID: 19660983
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
 
LVL 1

Author Comment

by:ayeen
ID: 19661955
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
 
LVL 11

Expert Comment

by:raj3060
ID: 19662895
Define the value in your form bean. It will default to the value you define there if one is provided.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:raj3060
ID: 19662908
So I am assuming that you have this:

private String selecteddriver = null;

Change it to:

private String selecteddriver = "PUT_YOUR _ DEFAULT_VALUE_HERE";
0
 
LVL 11

Expert Comment

by:raj3060
ID: 19662960
Also you should use this:

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

Did you try using LabelValueBean?
0
 
LVL 1

Author Comment

by:ayeen
ID: 19665063
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
 
LVL 11

Expert Comment

by:raj3060
ID: 19670074
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
 
LVL 14

Expert Comment

by:boonleng
ID: 19674744
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
 
LVL 1

Author Comment

by:ayeen
ID: 19686907
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
 
LVL 1

Author Comment

by:ayeen
ID: 19686913
raj3060:
how do i use the LabelValue Bean?
0
 
LVL 14

Expert Comment

by:boonleng
ID: 19689577
Where is you form submitted to? own action?
can you post the struts-config.xml.
0
 
LVL 1

Author Comment

by:ayeen
ID: 19693191
-----------------------------
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
 
LVL 14

Accepted Solution

by:
boonleng earned 500 total points
ID: 19696935
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
 
LVL 1

Author Comment

by:ayeen
ID: 19708376
geez! i can't believe it was just that simple *LOL*..thanks a lot boonleng!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
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.
Suggested Courses

810 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