You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Bob Langford <la...@silicon-masters.com> on 2003/10/11 19:35:22 UTC

How can I compute data for html:select choices?

Hi,  I can't seem to understand how to declare things correctly to do
what I want to do.  Can anyone tell me where I am going wrong?

I'm trying to use a simple Java method as the source of the choices
in a html:select box in a form.  The method returns a java.util.List,
where each item is a org.apache.struts.util.ValueLabelBean object, just
what I need for this.  Here's one way I tried to use it:
=============
<%@ page import="foo.bar.MyUtils" %>
<jsp:useBean id="months" type="java.util.List" />
<%  months = MyUtils.getMonthsList();   %>
   ...
    <html:select  ... >
      <html:options collection="months" property="value" 
labelProperty="label" />
    </html:select>
=============
The problem is that "useBean" looks up the attribute "months" in the page
context, and since it can't find it, throws an exception.  But without
the "useBean" tag, the "html:options" tag can't find the data it needs.
I've read the docs until my eyes hurt, and I can't find any technique to
tell "useBean" to merely create a new bean, not fetch an existing one.
This seems so easy, I can't believe I haven't done it before, but I can't
find an example in any of my previous code.

What am I missing?     Thanks in advance...

--
Bob Langford
Silicon Masters Consulting, Inc.    8207 Stone River Court, Richmond, VA  23235
phone:  804-674-1253      fax:  804-745-6650 
http://www.silicon-masters.com/  



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


Re: How can I compute data for html:select choices?

Posted by Vic Cekvenich <ce...@baseBeans.com>.
The point of MVC is to pre-populate the bean in Action.

You should unit test the bean in the model layer, once it works, put it 
in Struts. (MVC layered aproach allows for unit testing of a layer)
So if you say something like:
MyBean b = new MyBean();
b.populate();
Collection c = b.getOptions();
out.println(b);
-what do you get?
.V

A bean that does not work outside of Struts, will not work once you put 
it in Struts.


Daniel H. F. e Silva wrote:
> Hi Bob,
>  I think you could try <bean:define/> or, better, JSTL tag <c:set/>.
> I'm not a JSTL expert but i know you can determine scope to be used by <c:set/>.
> And i'm not sure, but i think you can do that also with <bean:define/>.
>  Hope it helped you.
> 
> Regards,
>  Daniel.
> 
> --- Bob Langford <la...@silicon-masters.com> wrote:
> 
>>Hi,  I can't seem to understand how to declare things correctly to do
>>what I want to do.  Can anyone tell me where I am going wrong?
>>
>>I'm trying to use a simple Java method as the source of the choices
>>in a html:select box in a form.  The method returns a java.util.List,
>>where each item is a org.apache.struts.util.ValueLabelBean object, just
>>what I need for this.  Here's one way I tried to use it:
>>=============
>><%@ page import="foo.bar.MyUtils" %>
>><jsp:useBean id="months" type="java.util.List" />
>><%  months = MyUtils.getMonthsList();   %>
>>   ...
>>    <html:select  ... >
>>      <html:options collection="months" property="value" 
>>labelProperty="label" />
>>    </html:select>
>>=============
>>The problem is that "useBean" looks up the attribute "months" in the page
>>context, and since it can't find it, throws an exception.  But without
>>the "useBean" tag, the "html:options" tag can't find the data it needs.
>>I've read the docs until my eyes hurt, and I can't find any technique to
>>tell "useBean" to merely create a new bean, not fetch an existing one.
>>This seems so easy, I can't believe I haven't done it before, but I can't
>>find an example in any of my previous code.
>>
>>What am I missing?     Thanks in advance...
>>
>>--
>>Bob Langford
>>Silicon Masters Consulting, Inc.    8207 Stone River Court, Richmond, VA  23235
>>phone:  804-674-1253      fax:  804-745-6650 
>>http://www.silicon-masters.com/  
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>
> 
> 
> 
> __________________________________
> Do you Yahoo!?
> The New Yahoo! Shopping - with improved product search
> http://shopping.yahoo.com

-- 
Victor Cekvenich,
Struts Instructor
(215) 312-9146

Advanced Struts Training
<http://basebeans.com/do/cmsPg?content=TRAINING> Server Side Java
training with Rich UI, mentoring, designs, samples and project recovery
in North East.
Simple best practice basic Portal, a Struts CMS, Membership, Forums,
Shopping and Credit processing, <http://basicportal.com> software, ready
to develop/customize; requires a db to run.



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


RE: How can I compute data for html:select choices?

Posted by Wiebe de Jong <wi...@shaw.ca>.
For a simple select like this one (months of the year), I use the following
code in my work. 

There is one file, AppStart.java, which contains a servlet that executes at
application start. The second part of the code shows what to add to web.xml
to make this execute. The last piece of code shows how to use the collection
in a jsp page.

Wiebe
http://frontierj.blogspot.com

--------------
AppStart.java:

package com.myco.myapp;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import org.apache.struts.util.LabelValueBean;

public class AppStart extends HttpServlet {

// a static representation of the MONTHS table
  private LabelValueBean[] monthTable = {
    new LabelValueBean("January", "1"),
    new LabelValueBean("February", "2"),
    new LabelValueBean("March", "3"),
    new LabelValueBean("April", "4"),
    new LabelValueBean("May", "5"),
    new LabelValueBean("June", "6"),
    new LabelValueBean("July", "7"),
    new LabelValueBean("August", "8"),
    new LabelValueBean("September", "9"),
    new LabelValueBean("October", "10"),
    new LabelValueBean("November", "11"),
    new LabelValueBean("December", "12")
  };
 
  // this will execute at startup
  public void init() throws ServletException {

    // month list
    ArrayList monthList = new ArrayList();
    for (int i = 0; i < monthTable.length; i++)
      monthList.add(i, monthTable[i]);
    getServletContext().setAttribute("appMonthList", monthList);
  }
}

--------------
web.xml:

  <servlet>
    <servlet-name>appstart</servlet-name>
    <servlet-class>com.myco.myapp.AppStart</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>0</param-value>
    </init-param>
    <load-on-startup>5</load-on-startup>
  </servlet>

--------------
page.jsp:

<tr>
  <td>Month:</td>
  <td><html:select property="month">
    <html:options collection="appMonthList" property="value"
labelProperty="label"/>
  </html:select></td>
</tr>

--------------

-----Original Message-----
From: Bob Langford [mailto:langford@silicon-masters.com] 
Sent: Saturday, October 11, 2003 10:35 AM
To: struts-user@jakarta.apache.org
Subject: How can I compute data for html:select choices?

Hi,  I can't seem to understand how to declare things correctly to do
what I want to do.  Can anyone tell me where I am going wrong?

I'm trying to use a simple Java method as the source of the choices
in a html:select box in a form.  The method returns a java.util.List,
where each item is a org.apache.struts.util.ValueLabelBean object, just
what I need for this.  Here's one way I tried to use it:
=============
<%@ page import="foo.bar.MyUtils" %>
<jsp:useBean id="months" type="java.util.List" />
<%  months = MyUtils.getMonthsList();   %>
   ...
    <html:select  ... >
      <html:options collection="months" property="value" 
labelProperty="label" />
    </html:select>
=============
The problem is that "useBean" looks up the attribute "months" in the page
context, and since it can't find it, throws an exception.  But without
the "useBean" tag, the "html:options" tag can't find the data it needs.
I've read the docs until my eyes hurt, and I can't find any technique to
tell "useBean" to merely create a new bean, not fetch an existing one.
This seems so easy, I can't believe I haven't done it before, but I can't
find an example in any of my previous code.

What am I missing?     Thanks in advance...

--
Bob Langford
Silicon Masters Consulting, Inc.    8207 Stone River Court, Richmond, VA
23235
phone:  804-674-1253      fax:  804-745-6650 
http://www.silicon-masters.com/  



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


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


Re: How can I compute data for html:select choices?

Posted by "Daniel H. F. e Silva" <dh...@yahoo.com>.
Hi Bob,
 I think you could try <bean:define/> or, better, JSTL tag <c:set/>.
I'm not a JSTL expert but i know you can determine scope to be used by <c:set/>.
And i'm not sure, but i think you can do that also with <bean:define/>.
 Hope it helped you.

Regards,
 Daniel.

--- Bob Langford <la...@silicon-masters.com> wrote:
> Hi,  I can't seem to understand how to declare things correctly to do
> what I want to do.  Can anyone tell me where I am going wrong?
> 
> I'm trying to use a simple Java method as the source of the choices
> in a html:select box in a form.  The method returns a java.util.List,
> where each item is a org.apache.struts.util.ValueLabelBean object, just
> what I need for this.  Here's one way I tried to use it:
> =============
> <%@ page import="foo.bar.MyUtils" %>
> <jsp:useBean id="months" type="java.util.List" />
> <%  months = MyUtils.getMonthsList();   %>
>    ...
>     <html:select  ... >
>       <html:options collection="months" property="value" 
> labelProperty="label" />
>     </html:select>
> =============
> The problem is that "useBean" looks up the attribute "months" in the page
> context, and since it can't find it, throws an exception.  But without
> the "useBean" tag, the "html:options" tag can't find the data it needs.
> I've read the docs until my eyes hurt, and I can't find any technique to
> tell "useBean" to merely create a new bean, not fetch an existing one.
> This seems so easy, I can't believe I haven't done it before, but I can't
> find an example in any of my previous code.
> 
> What am I missing?     Thanks in advance...
> 
> --
> Bob Langford
> Silicon Masters Consulting, Inc.    8207 Stone River Court, Richmond, VA  23235
> phone:  804-674-1253      fax:  804-745-6650 
> http://www.silicon-masters.com/  
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

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