• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drop down box in Struts

 
Ash Acharya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the optionsCollection tag rather than options. The tag would look like this:
 
Ash Acharya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah! got it! thanks a lot Merrill!
 
Ash Acharya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic