You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by kunla <ma...@gmail.com> on 2009/04/20 08:15:05 UTC

creating list in dropdown using struts


 creating list in dropdown using struts :

In action class
list.add(new LabelValueBean("ID","Name"));

In Jsp
<logic:present name="list" scope="session">
<td align="left">*&nbsp;Select Item</td>
<td>
<html:select  property="selectedItem">
<html:option value="-1">Select</html:option>	
<html:options collection="list" property="label" labelProperty="value" />
</html:select>
</td>
</tr>
<tr></tr>
</logic:present> 

In Form :
getter and setter for selectedItem.

query : Now I want to retrieve ID and Name both in action class.I am able to
retrieve ID,But i want to retrieve both. 

-- 
View this message in context: http://www.nabble.com/creating-list-in-dropdown-using-struts-tp23130937p23130937.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: creating list in dropdown using struts

Posted by Muthu Velappan <mu...@aspiresys.com>.
I don't think there is any way to get both label and text from select box in
request option. You will get only the value specified in <option> tag. I
believe in ur case, ID is the value in <option> tag and that's the reason u
get Id back in action during postback...

There is couple of workaround's to overcome this. I think this is how ur
<select> tag will look like now

<Select name="cmbNames">
	<Option value="1">John</Option>
	<Option value="2">Mark</Option>	
	<Option value="3">Carrie</Option>	
</Select>

So when you call request.getParameter("cmbNames"), you will get the id of
the selected name. If John is selected, it will be 1.

Workaround:

Option 1: With this Id value "1", you can call a helper class/method to
retrive the name. This is the correct approach in most of the cases.

Option 2: While building the data in combo box itself build a parameter
which hold both Id & Name with some separator like | or ^. 

For ex, this is how you should build ur List,

ArrayList < LabelValueBean > list = new ArrayList< LabelValueBean >(5);
list.add(new LabelValueBean("1|John","John"));
list.add(new LabelValueBean("2|Mark","Mark"));
list.add(new LabelValueBean("3|Carrie","Carrie"));

Now, label value bean contains both id & name in one property itself, set
that to <option> tag's value. So, your new select tag would be like this

<Select name="cmbNames">
	<Option value="1|John">John</Option>
	<Option value="2|Mark">Mark</Option>	
	<Option value="3|Carrie">Carrie</Option>	
</Select>

Now, when you call request.getParameter("cmbNames") on post back, you will
get the value of the selected name. If John is selected, it will be
"1|John", split that content u will get both id and name of selected option.
This is crooked but works fine for small apps.

Hope this helps you.

~Muthu

-----Original Message-----
From: kunla [mailto:manish.mnit1985@gmail.com] 
Sent: Monday, April 20, 2009 11:45 AM
To: user@struts.apache.org
Subject: creating list in dropdown using struts



 creating list in dropdown using struts :

In action class
list.add(new LabelValueBean("ID","Name"));

In Jsp
<logic:present name="list" scope="session">
<td align="left">*&nbsp;Select Item</td>
<td>
<html:select  property="selectedItem">
<html:option value="-1">Select</html:option>	
<html:options collection="list" property="label" labelProperty="value" />
</html:select>
</td>
</tr>
<tr></tr>
</logic:present> 

In Form :
getter and setter for selectedItem.

query : Now I want to retrieve ID and Name both in action class.I am able to
retrieve ID,But i want to retrieve both. 

-- 
View this message in context:
http://www.nabble.com/creating-list-in-dropdown-using-struts-tp23130937p2313
0937.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org