You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Craig R. McClanahan" <Cr...@eng.sun.com> on 2000/09/05 21:45:34 UTC

Re: Using link tag with enumerate

Nilsa Gonzalez wrote:

> Hi,
>
> I am using an enumerate tag which uses a Hashtable to get its content. It generates
> a table of objects. I wish to create a link for each object such that when you
> click on the name, the object's properties/details are shown in another frame. for
> this I wished to append the value of the key (of the Hashtable) to the URL in the
> href field of the link tag. The documentation says that the key value gets appended
> to the URl and is passed as query parameter. But it does not work for me. I noticed
> that the struts-example uses a custom "linkSubscription" tag. Is it possible to do
> it through struts link tag?
>

With the current version of Struts, you've got the following options:

* Use an app-specific custom tag like the example app does (one
  reason I chose this path is so that the page developer does not
  need to know what query parameters are actually required to look
  up the information in the linked-to page).

* Use a scriptlet expression to calculate the value for the "href"
  attribute, which accepts runtime expression values.  (Yes, I know,
  yuck, but it is possible).

* Store the parameter name(s) and corresponding value(s) that identify
  the specific record you will want into a Map, and specify that map
  using the "name" (or "name" and "property") attributes of the link tag.
  See below for an example.

A recent suggestion on STRUTS-USER, which has not yet been implemented, is to add new
optional attributes that allow you to specify the name and value of a specific request
parameter that will be added to the hyperlink dynamically.  This covers the simplest
case where you only need one parameter -- the Map based approach is still available for
when multiple parameters are required.

As an example of the Map case, let's assume you're iterating through a set of Customer
objects (enumeration is the same concept for JDK 1.1 -- just use the <struts:link1> tag
instead of <struts:link>).  Further, assume that your Customer object includes a
method:

    public Map getKeys();

that returns a Map containing the names and values of the request parameters that need
to be included in the hyperlink.  It might return a key of "customer_id" and a value of
"123" (the customer identifier of this particular customer), for example.  Then, when
you are iterating through, you can create dynamic hyperlinks like this (assume that
"customers" contains a collection of Customer objects):

    <struts:iterate id="customer" name="customers">
        <struts:link href="/selectCustomer.do"
         name="customer" property="keys">
            This is a link for customer
            <struts:property name="customer" property="name"/>
        </struts:link>
    </struts:iterate>

and each hyperlink will be customized by adding the request parameter names and values
returned by getKeys().

>
> Thanks,
> Nena

Craig McClanahan

====================
See you at ApacheCon Europe <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00):  Sun Technical Briefing
Session T06  (24-Oct 14h00-15h00):  Migrating Apache JServ
                                    Applications to Tomcat