struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Gordon <adam.gor...@readytalk.com>
Subject Re: Tags in struts
Date Mon, 10 Jul 2006 20:46:08 GMT
Maya-

Let's say your TestBean class has getFoo() and getBar() methods both 
which return Collection objects.  If these objects are instances of 
LabelValueBean objects it's really easy, if they are not, I'd recommend 
converting them to these bean objects either in your bean class or in 
the action (though the latter could add additional overhead if not done 
correctly).  Assume also that your TestBean class has getter and setter 
methods for ONE Foo and ONE Bar (say getOneFoo(), setOneFoo(String foo), 
getOneBar(), setOneBar(String bar).

In your Action's execute method (or a method called by execute(...)) 
type (assume testBean is an instance of the TestBean class and the 
getFoo() and getBar() methods return Collections of LabelValueBean objects):

  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
    throws Exception {

       ...

       Collection fooCollection = testBean.getFoo();
       Collection barCollection = testBean.getBar();

       request.setAttribute("fooCollection", fooCollection);  // 
"fooCollection" can be any text you want - i recommend using a final 
static reference instead of a hard-coded string.

       request.setAttribute("barCollection", barCollection); // same 
comment applies

       ...
    }

in your JSP, you would say:

        <html:select property="oneFoo"> <!-- the get/setOneFoo() method -->
          <html:options collection="fooCollection"  <!-- the name of 
your collection on the request -->
                                   property="value"
                                   labelProperty="label"/>
        </html:select>

and,

        <html:select property="oneBar">
          <html:options collection="barCollection"
                                   property="value"
                                   labelProperty="label"/>
        </html:select>

to render list boxes of your model data.

html:options knows that you have a Collection of objects (in this case 
LabelValueBean objects) that have the methods getValue() and 
getLabel().  Note that since these attributes are configurable, you can 
make the Collection a collection of any type of bean you want so long as 
all the beans in the collection have a getXXX() methods where XXX are 
the values of the attributes "property" and "labelProperty".

When your form is submitted, the setOneFoo(String) and setOneBar(String) 
methods will be called and set with the value of the "property" 
attribute.  the "labelProperty" attribute dictates what is actually 
displayed to the user in the browser.

Let me know if you have any more questions.

-Adam

Maya menon wrote:
> Thanks Adam.
>    
>   Yes, I have a bean lets say, Testbean with two Collections.
>    
>   I have to display the value of these Collections[has objects] in a
> select box in my JSP.
>    
>   Can you send me the sample code please ?
>    
>   Maya
>
>   

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


Mime
View raw message