You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Phillip Rhodes <ph...@rhoderunner.com> on 2001/03/17 05:33:43 UTC

populate select box from formbean collection?

In my struts jsp page, I have a form.  It's corresponding formbean is in 
the request via my action class.  My formbean has a property of type 
collection that I would like to use to populate a select box.  I have done 
it a couple of ways, but it's real messy.

Does anybody have a more elegant solution?  Both ways below work.  but ugh!

Thanks!

<%
   //get the formbean out of request
   com.healthgauge.UserForm uf = 
(com.healthgauge.UserForm)request.getAttribute("userForm");	
   //get the collection and stuff in pageContext
   pageContext.setAttribute("companies",uf.getCompanies());
%>
       <html:select property="companyId">
         <html:options collection="companies" property="id"
                    labelProperty="companyName"/>
       </html:select>
vs.
       <html:select property="companyId">
<logic:iterate id="company" name="userForm" property="companies">
<option value="<bean:write name="company" property="id" filter="true"/>">
<bean:write name="company" property="companyName" filter="true"/>
</logic:iterate>
       </html:select>


Re: populate select box from formbean collection?

Posted by David Winterfeldt <dw...@yahoo.com>.
The scope value is optional.

<bean:define id="companies" name="userForm"
             property="companies" scope="request"/>
<html:select property="companyId">
  <html:options collection="companies" property="id"
                labelProperty="companyName"/>
</html:select>

David

--- Phillip Rhodes <ph...@rhoderunner.com> wrote:
> In my struts jsp page, I have a form.  It's
> corresponding formbean is in 
> the request via my action class.  My formbean has a
> property of type 
> collection that I would like to use to populate a
> select box.  I have done 
> it a couple of ways, but it's real messy.
> 
> Does anybody have a more elegant solution?  Both
> ways below work.  but ugh!
> 
> Thanks!
> 
> <%
>    //get the formbean out of request
>    com.healthgauge.UserForm uf = 
>
(com.healthgauge.UserForm)request.getAttribute("userForm");
> 
>    //get the collection and stuff in pageContext
>   
>
pageContext.setAttribute("companies",uf.getCompanies());
> %>
>        <html:select property="companyId">
>          <html:options collection="companies"
> property="id"
>                     labelProperty="companyName"/>
>        </html:select>
> vs.
>        <html:select property="companyId">
> <logic:iterate id="company" name="userForm"
> property="companies">
> <option value="<bean:write name="company"
> property="id" filter="true"/>">
> <bean:write name="company" property="companyName"
> filter="true"/>
> </logic:iterate>
>        </html:select>
> 


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/