Return-Path: Mailing-List: contact struts-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list struts-user@jakarta.apache.org Received: (qmail 48514 invoked from network); 12 Sep 2000 11:01:33 -0000 Received: from anchor-post-30.mail.demon.net (194.217.242.88) by locus.apache.org with SMTP; 12 Sep 2000 11:01:33 -0000 Received: from jimsoft.demon.co.uk ([194.222.64.85] helo=pc450) by anchor-post-30.mail.demon.net with smtp (Exim 2.12 #1) id 13Ynoc-000DHa-0U for struts-user@jakarta.apache.org; Tue, 12 Sep 2000 12:01:27 +0100 Message-ID: <01f501c01ca8$a532b0a0$5540dec2@pc450> From: "James Strachan" To: References: <40D573F8B48BD21188F20060973687E1011CFAB4@ndntsrv01.novadata.de> Subject: Re: struts:link tag Date: Tue, 12 Sep 2000 12:00:20 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_01F1_01C01CB1.06507860" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 X-Spam-Rating: locus.apache.org 1.6.2 0/1000/N This is a multi-part message in MIME format. ------=_NextPart_000_01F1_01C01CB1.06507860 Content-Type: multipart/alternative; boundary="----=_NextPart_001_01F2_01C01CB1.06507860" ------=_NextPart_001_01F2_01C01CB1.06507860 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable RE: struts:link tag Till Here's the source code for my copy of the LinkTag.java if you want to = try it out. You'll need to add the following extra attribute to your = struts.tld file too as I've added a new attribute "id" to LinkTag for = the optional name of the query parameter. If you don't use the id = attribute then the bean property name is used for the query parameter = name. id false false Regards James James Strachan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ web: http://www.metastuff.com mail: james@metastuff.com ----- Original Message -----=20 From: Otto, Till=20 To: 'struts-user@jakarta.apache.org'=20 Sent: 12 September 2000 10:03 Subject: RE: struts:link tag Hi James=20 Excellent, thank you. Will this go into the main distribution (any = time soon)?=20 -Till=20 > -----Original Message-----=20 > From: James Strachan [mailto:james@metastuff.com]=20 > Sent: Tuesday, September 12, 2000 10:40 AM=20 > To: struts-user@jakarta.apache.org=20 > Subject: Re: struts:link tag=20 >=20 >=20 > struts:link tagHi Till=20 >=20 > I sent a patch to the struts-dev list to do exactly that.=20 > Here are the mails=20 > with a patch to do as you need=20 >=20 > http://archive.covalent.net/jakarta/struts-dev/2000/09/0004.xml=20 > http://archive.covalent.net/jakarta/struts-dev/2000/09/0003.xml=20 >=20 > Regards=20 > James=20 >=20 > James Strachan=20 > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~=20 > web: http://www.metastuff.com=20 > mail: james@metastuff.com=20 >=20 > ----- Original Message -----=20 > From: Otto, Till=20 > To: 'struts-user@jakarta.apache.org'=20 > Sent: 12 September 2000 08:33=20 > Subject: struts:link tag=20 >=20 >=20 > Hello!=20 > I would like to display a list of items with hyperlinks to an=20 > "edit page"=20 > for each line item. Just like the subscription section in=20 > registration.jsp=20 > of the struts-example application.=20 > The example uses a custom tag for this purpose. This seems=20 > to be an awful=20 > lot of work for this common task, doesn't it? The only=20 > alternative I found=20 > is to use the "map feature" of the struts:link tag via the=20 > name and property=20 > attributes.=20 > Wouldn't it be nice to have another attribute in the=20 > struts:link tag, e.g.=20 > "param" that specified the property of a bean given in the=20 > name attribute?=20 > The name and value of the specified property would then be=20 > appended to the=20 > href of the hyperlink.=20 > Like this:=20 > param=3D"id">=20 > My subscription=20 > If a subscription bean with a property "id" existed and id's=20 > value were=20 > 1001, this would evaluate to=20 > =20 > My subscription=20 >=20 >=20 > Does this make sense? Hope it wasn't too confusing.=20 > -Till=20 >=20 ------=_NextPart_001_01F2_01C01CB1.06507860 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable RE: struts:link tag
 
Till
 
Here's the source code for my copy of the = LinkTag.java if you=20 want to try it out. You'll need to add the following extra attribute to = your=20 struts.tld file too as I've added a new attribute "id" to LinkTag for = the=20 optional name of the query parameter. If you don't use the id attribute = then the=20 bean property name is used for the query parameter name.
 
   =20 <attribute>
     =20 <name>id</name>
     =20 <required>false</required>
      = <rtexprvalue>false</rtexprvalue>
   =20 </attribute>
Regards
James
 
James = Strachan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
web:=20
http://www.metastuff.com
mail: = james@metastuff.com
----- Original Message -----
From:=20 Otto,=20 Till
To: 'struts-user@jakarta.apa= che.org'=20
Sent: 12 September 2000 = 10:03
Subject: RE: struts:link = tag

Hi James
Excellent, thank = you. =20 Will this go into the main distribution (any time soon)? =
-Till

> -----Original Message-----
>=20 From: James Strachan [mailto:james@metastuff.com]=20
> Sent: Tuesday, September 12, 2000 10:40 = AM=20
> To: struts-user@jakarta.apache= .org=20
> Subject: Re: struts:link tag
>

>
>=20 struts:link tagHi Till
>
> I sent a patch to the struts-dev list to do exactly = that.=20
> Here are the mails
>=20 with a patch to do as you need
> =
> = http://archive.covalent.net/jakarta/struts-dev/2000/09/0004.xml=20
> = http://archive.covalent.net/jakarta/struts-dev/2000/09/0003.xml=20
>
> Regards =
> James

>
>=20 James Strachan
>=20 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> web: = http://www.metastuff.com =
> mail: james@metastuff.com
> =
> ----- Original Message ----- =
> From: Otto, Till
> To:=20 'struts-user@jakarta.apache.org'
> Sent: = 12=20 September 2000 08:33
> Subject: = struts:link=20 tag
>
> =
> Hello!
> I would like to = display a list=20 of items with hyperlinks to an
> "edit = page"=20
> for each line item.  Just like the = subscription=20 section in
> registration.jsp =
> of the struts-example application.
>=20 The example uses a custom tag for this purpose.  This seems=20
> to be an awful
> lot=20 of work for this common task, doesn't it?  The only =
> alternative I found
> is to = use the=20 "map feature" of the struts:link tag via the
> name=20 and property
> attributes. =
> Wouldn't it be nice to have another attribute in the=20
> struts:link tag, e.g.
> "param" that specified the property of a bean given in = the=20
> name attribute?
> The=20 name and value of the specified property would then be =
> appended to the
> href of = the=20 hyperlink.
> Like this:
> <struts:link href=3D"editSubscription.do" = name=3D"subscription"=20
> param=3D"id">
> My=20 subscription</struts:link>
> If a=20 subscription bean with a property "id" existed and id's =
> value were
> 1001, this = would evaluate=20 to
> <a=20 href=3D"editSubscription.do?id=3D1001">
> My=20 subscription</a>
>
>
> Does this make sense? = Hope it wasn't=20 too confusing.
> -Till
>=20

------=_NextPart_001_01F2_01C01CB1.06507860-- ------=_NextPart_000_01F1_01C01CB1.06507860 Content-Type: application/octet-stream; name="LinkTag.java" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="LinkTag.java" /* * $Header: = /home/cvspublic/jakarta-struts/src/share/org/apache/struts/taglib/LinkTag= .java,v 1.11 2000/08/14 04:42:51 craigmcc Exp $ * $Revision: 1.11 $ * $Date: 2000/08/14 04:42:51 $ * * = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D * * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software = itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products = derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * */ package org.apache.struts.taglib; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URLEncoder; import java.util.Iterator; import java.util.Map; import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionForwards; import org.apache.struts.util.BeanUtils; import org.apache.struts.util.MessageResources; /** * Generate a URL-encoded hyperlink to the specified URI. * * @author Craig R. McClanahan * @version $Revision: 1.11 $ $Date: 2000/08/14 04:42:51 $ */ public class LinkTag extends TagSupport { // ----------------------------------------------------- Instance = Variables /** * The logical forward name from which to retrieve the hyperlink = URI. */ protected String forward =3D null; /** * The hyperlink URI. */ protected String href =3D null; /** * The message resources for this package. */ protected static MessageResources messages =3D MessageResources.getMessageResources ("org.apache.struts.taglib.LocalStrings"); /** * The JSP bean name for query parameters. */ protected String name =3D null; /** * The JSP bean property name for query parameters. */ protected String property =3D null; /** * The window target. */ protected String target =3D null; /** * The query id to use for a single bean property. */ protected String id =3D null; // ------------------------------------------------------------- = Properties /** * Return the logical forward name. */ public String getForward() { return (this.forward); } /** * Set the logical forward name. * * @param forward The new logical forward name */ public void setForward(String forward) { this.forward =3D forward; } /** * Return the hyperlink URI. */ public String getHref() { return (this.href); } /** * Set the hyperlink URI. * * @param href Set the hyperlink URI */ public void setHref(String href) { this.href =3D href; } /** * Return the bean name. */ public String getName() { return (this.name); } /** * Set the bean name. * * @param name The bean name */ public void setName(String name) { this.name =3D name; } /** * Return the property name. */ public String getProperty() { return (this.property); } /** * Set the property name. * * @param property The property name */ public void setProperty(String property) { this.property =3D property; } /** * Return the window target. */ public String getTarget() { return (this.target); } /** * Set the window target. * * @param target The new window target */ public void setTarget(String target) { this.target =3D target; } =20 =20 /** * Return the query id to use for a single bean property. */ public String getId() { return (this.id); } /** * Set the query id to use for a single bean property. * * @param target The new query id to use for a single bean property */ public void setId(String id) { this.id =3D id; } // --------------------------------------------------------- Public = Methods /** * Render the beginning of the hyperlink. * * @exception JspException if a JSP exception has occurred */ public int doStartTag() throws JspException { // Validate our attributes if ((forward =3D=3D null) && (href =3D=3D null)) throw new JspException (messages.getMessage("linkTag.destination")); else if ((forward !=3D null) && (href !=3D null)) throw new JspException (messages.getMessage("linkTag.destination")); // Generate the hyperlink start element HttpServletResponse response =3D (HttpServletResponse) pageContext.getResponse(); StringBuffer results =3D new StringBuffer(""); // Print this element to our output writer JspWriter writer =3D pageContext.getOut(); try { writer.print(results.toString()); } catch (IOException e) { throw new JspException (messages.getMessage("common.io", e.toString())); } // Evaluate the body of this tag return (EVAL_BODY_INCLUDE); } /** * Render the end of the hyperlink. * * @exception JspException if a JSP exception has occurred */ public int doEndTag() throws JspException { // Print the ending element to our output writer JspWriter writer =3D pageContext.getOut(); try { writer.print(""); } catch (IOException e) { throw new JspException (messages.getMessage("common.io", e.toString())); } return (EVAL_PAGE); } /** * Release any acquired resources. */ public void release() { super.release(); forward =3D null; href =3D null; name =3D null; property =3D null; target =3D null; } // ------------------------------------------------------ Protected = Methods /** * Return the specified hyperlink, modified as necessary with = optional * request parameters. * * @exception JspException if an error occurs preparing the = hyperlink */ protected String hyperlink() throws JspException { String href =3D this.href; // If "forward" was specified, compute the "href" to forward to if (forward !=3D null) { ActionForwards forwards =3D (ActionForwards) pageContext.getAttribute(Action.FORWARDS_KEY, PageContext.APPLICATION_SCOPE); if (forwards =3D=3D null) throw new JspException (messages.getMessage("linkTag.forwards")); ActionForward forward =3D forwards.findForward(this.forward); if (forward =3D=3D null) throw new JspException (messages.getMessage("linkTag.forward")); HttpServletRequest request =3D (HttpServletRequest) pageContext.getRequest(); href =3D request.getContextPath() + forward.getPath(); } // Just return the "href" attribute if there is no bean to look up if ((property !=3D null) && (name =3D=3D null)) throw new JspException (messages.getMessage("getter.name")); if (name =3D=3D null) return (href); // Look up the map we will be using Object bean =3D pageContext.findAttribute(name); if (bean =3D=3D null) throw new JspException (messages.getMessage("getter.bean", name)); Map map =3D null; if (property =3D=3D null) { try { map =3D (Map) bean; } catch (ClassCastException e) { throw new JspException (messages.getMessage("linkTag.type")); } } else { try { Object value =3D BeanUtils.getPropertyValue(bean, = property); if (value =3D=3D null) { throw new JspException (messages.getMessage("getter.property", property)); } else if ( value instanceof Map ) { map =3D (Map) value; } else { // lets use a Map of one key and value map =3D new HashMap(); String key =3D getId(); if ( key =3D=3D null ) { key =3D property; } map.put( key, value ); } } catch (IllegalAccessException e) { throw new JspException (messages.getMessage("getter.access", property, name)); } catch (InvocationTargetException e) { Throwable t =3D e.getTargetException(); throw new JspException (messages.getMessage("getter.result", property, t.toString())); } catch (ClassCastException e) { throw new JspException (messages.getMessage("linkTag.type")); } catch (NoSuchMethodException e) { throw new JspException (messages.getMessage("getter.method", property, name)); } } // Append the required query parameters StringBuffer sb =3D new StringBuffer(href); boolean question =3D (href.indexOf("?") >=3D 0); Iterator keys =3D map.keySet().iterator(); while (keys.hasNext()) { String key =3D (String) keys.next(); Object value =3D map.get(key); if (value instanceof String[]) { String values[] =3D (String[]) value; for (int i =3D 0; i < values.length; i++) { if (question) sb.append('&'); else { sb.append('?'); question =3D true; } sb.append(key); sb.append('=3D'); sb.append(URLEncoder.encode(values[i])); } } else { if (question) sb.append('&'); else { sb.append('?'); question =3D true; } sb.append(key); sb.append('=3D'); sb.append(URLEncoder.encode(value.toString())); } } // Return the final result return (sb.toString()); } } ------=_NextPart_000_01F1_01C01CB1.06507860--