You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Otto, Frank" <ot...@delta-barth.de> on 2004/02/02 13:11:09 UTC

html:options and html:select

hello,
 
I have following bean:
 
public class CProduct {
 
    private Integer id;
 
    private String name
 
    public Integer getId() {return id;}
    puclic void setId(Integer id){
...
}
 
My form class contains a java.util.List of CProduct bean.
 
How can I display the List as select box? I want to use the id as value and the name as label of the select box.
 
For example:
 
<select name="ref">
    <option value="1">scanner</option>
    <option value="2">harddisk</option>
</select>
 
Has anyone an idea?
 
Regards,
 
Frank
 

Re: html:options and html:select

Posted by Claire Wall <cl...@kurtosys.com>.
you can do the following:

<html:select name="FormName" property="xxx">
    <logic:iterate id="product" name="FormName" property="ListName">
        <option value="<bean:write name="product"
property="id"/>"><bean:write name="product" property="name"/>
    </logic:iterate>
</html:select>


'property' of the select box should be the name of the property in your
FormBean that you wish to store the selected value in.
You then iterate through the list in your form by using the <logic:iterate>
tag. For every object in your list an <option> tag is written, where the
value is the property 'id' of the current CProduct and the name displayed in
the select box is the name of this CProduc object. This may not work as it
stands due to using an Integer object - i'm not certain that you can set the
value of a select box option to an Integer object. You'd have to try it and
see, and perhaps somebody can clarify this further, but I've never tried
this myself.

HTH
claire :)

----- Original Message -----
From: "Otto, Frank" <ot...@delta-barth.de>
To: "Struts-User (E-Mail)" <st...@jakarta.apache.org>
Sent: Monday, February 02, 2004 12:11 PM
Subject: html:options and html:select


> hello,
>
> I have following bean:
>
> public class CProduct {
>
>     private Integer id;
>
>     private String name
>
>     public Integer getId() {return id;}
>     puclic void setId(Integer id){
> ...
> }
>
> My form class contains a java.util.List of CProduct bean.
>
> How can I display the List as select box? I want to use the id as value
and the name as label of the select box.
>
> For example:
>
> <select name="ref">
>     <option value="1">scanner</option>
>     <option value="2">harddisk</option>
> </select>
>
> Has anyone an idea?
>
> Regards,
>
> Frank
>
>



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


Re: html:options and html:select

Posted by Mark Lowe <ma...@talk21.com>.

ArrayList productList = new ArrayList();

CProduct product = new CProduct();
product.setId(new Integer(1));
product.setName("My Product");

productList.add(product);

request.setAttribute("products",productList.toArray());

...

<html:option value="">--</html:option>
<html:options collection="products" property="id" labelProperty="name" 
/>


If you dont want to have a refering action then you can have an action 
that returns null rather than a forward. and do this in jstl

<c:import url="/product/list.do" />

Cheers Mark


On 2 Feb 2004, at 13:11, Otto, Frank wrote:

> hello,
>
> I have following bean:
>
> public class CProduct {
>
>     private Integer id;
>
>     private String name
>
>     public Integer getId() {return id;}
>     puclic void setId(Integer id){
> ...
> }
>
> My form class contains a java.util.List of CProduct bean.
>
> How can I display the List as select box? I want to use the id as 
> value and the name as label of the select box.
>
> For example:
>
> <select name="ref">
>     <option value="1">scanner</option>
>     <option value="2">harddisk</option>
> </select>
>
> Has anyone an idea?
>
> Regards,
>
> Frank
>


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