File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Drop down box in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Drop down box in Struts" Watch "Drop down box in Struts" New topic
Author

Drop down box in Struts

Ash Acharya
Greenhorn

Joined: Jan 09, 2009
Posts: 11
Hi all,
I'm developing an application in Struts, where I need to have drop downs in my jsp. I'm using treemap in my form bean's reset() to populate the drop down in the jsp. the problem is, how can I get to the individual values in the drop down? here's my code..

jsp:
-----
<tr>
<td>Select your hobby:</td>
<td>
<html:select property="hobby">
<htmlptions property="keySetHobby" labelProperty="hobbyList"></htmlptions>
</html:select>
</td>
</tr>

form:
--------
public class LoginForm extends org.apache.struts.action.ActionForm {

private String hobby;
private Collection hobbyList;
private Set keySetHobby;
private String key;
private Map hobbyMap;

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public Map getHobbyMap() {
return hobbyMap;
}

public void setHobbyMap(Map hobbyMap) {
this.hobbyMap = hobbyMap;
}

public Set getKeySetHobby() {
return keySetHobby;
}

public void setKeySetHobby(Set keySetHobby) {
this.keySetHobby = keySetHobby;
}

public String getHobby() {
return hobby;
}

public void setHobby(String hobby) {
this.hobby = hobby;
}

public Collection getHobbyList() {
return hobbyList;
}

public void setHobbyList(Collection hobbyList) {
this.hobbyList = hobbyList;
}

public LoginForm() {
super();
// TODO Auto-generated constructor stub
}

@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
super.reset(mapping, request);

Map<String,String> hobbyTempMap=new TreeMap<String,String>();

hobbyTempMap.put("-1","Select");
hobbyTempMap.put("0","Eating");
hobbyTempMap.put("1","Sleeping");
hobbyTempMap.put("2","Drinking");
this.setHobbyMap(hobbyTempMap);
this.setHobbyList(hobbyTempMap.values());
Set keys=hobbyTempMap.keySet();
this.keySetHobby=keys;
}

@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

if (this.getHobby().equals("Select"))
errors.add("keySetHobby", new ActionMessage("error.KeySetHobby.required"));
return errors;
}
}

I'm keenly looking forward for help.

regards,
Ash.


regards,
Ash.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Use the optionsCollection tag rather than options. The tag would look like this:


Merrill
Consultant, Sima Solutions
Ash Acharya
Greenhorn

Joined: Jan 09, 2009
Posts: 11
Thank you Merrill.. I got the drop down working, but the selection it returns in the success page is wrong, and the corresponding value it returns is also wrong. Any suggestions on where I'm going wrong? Here's the code in my success.jsp.

<p>Your selection is:<bean:write name="LoginForm" property="hobby" /></p> ---- This prints the value associated with my
display text in the drop down.
<p>The value of your selection is:<bean:write name="LoginForm" property="key" /></p> --- This prints nothing!

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You've specified a code and a label for each code. What gets submitted to the server when the user presses the submit button is only the code, not the label. So, for example, if the user selected "Sleeping", the value of the tag should be "1". If you want the label "Sleeping" to appear, you'll have to translate it using the map.
Ash Acharya
Greenhorn

Joined: Jan 09, 2009
Posts: 11
yeah! got it! thanks a lot Merrill!
Ash Acharya
Greenhorn

Joined: Jan 09, 2009
Posts: 11
Hi,
In reference to the code above, I've another situation where I have to access data in one form bean in another form bean. Currently, I'm able to access the HobbyMap which is in one form bean in the other form bean, but I'm not able to get to its selected value. Here's my code in the other form bean, which I use to access the data in the first form bean:

Second formBean:
-----------------
System.out.println(LoginForm.getHobbyMap.get(LoginForm.getHobbyMap.getHobbyKey());

Any help regarding this please? Thanks in advance.

regards,
Ash.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drop down box in Struts
 
Similar Threads
Problem with html:options
html:select html:options error, no getter. please help
html:select and html:options
populate <html:options> with AJAX response
html:select and html:options