You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by James Strachan <ja...@metastuff.com> on 2000/09/01 18:35:33 UTC

Patch: LinkTag for a single bean property

I've just started using Struts and have just subscribed to this list so excuse me if this has been asked/answered before - I couldn't find a faq / archive of this list anywhere. (I'm trawling through the ezm lists which is a bit grim).

I've attached a small patch to LinkTag.java which allows it to be used to construct a link when only one query argument is needed to be applied to a URL. I've found this to be particularly useful when using something like <struts:iterate>. 

For example consider the following JSP:-

<%
  Collection teams = ..;
  pageContext = userModel.getUserTeams( username );
  if ( teams != null && ! teams.isEmpty() ) {
    pageContext.setAttribute( "teams", teams );
%>


  <struts:iterate id="bean" name="collectionOfBeans">
  <tr>    
    <td align="left"><struts:htmlProperty name="bean" property="id"/>    </td>
    <td align="left"><struts:htmlProperty name="bean" property="name"/></td>
    <td align="center">
      <struts:link href="editBean.do?action=Edit" id="bean_id" name="bean" property="id">
            <struts:message key="edit.bean"/>
      </struts:link>
    </td>

It would generate the following hrefs in the table

    editBean.do?action=Edit&bean_id=1
    editBean.do?action=Edit&bean_id=2
    editBean.do?action=Edit&bean_id=3

Note that the new 'id' tag attribute in the call of <struts:link> is optional to allow the property name to be redefined to the name of the query argument in cases where the property and URL parameter names differ.

If there is a more elegant way of doing the above let me know - I must have missed it. Otherwise this makes the <struts:link> tag more flexible & reusable, as adding a single query argument is quite common. 

Regards
James

James Strachan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
web: http://www.metastuff.com
mail: james@metastuff.com


Re: Patch: LinkTag for a single bean property

Posted by James Strachan <ja...@metastuff.com>.
Whoops, ignore the extra stuff at the top of the example JSP, I cut n pasted by mistake. The example should have read:-

  <struts:iterate id="bean" name="collectionOfBeans">
  <tr>    
    <td align="left"><struts:htmlProperty name="bean" property="id"/>    </td>
    <td align="left"><struts:htmlProperty name="bean" property="name"/></td>
    <td align="center">
      <struts:link href="editBean.do?action=Edit" id="bean_id" name="bean" property="id">
            <struts:message key="edit.bean"/>
      </struts:link>
    </td>
  </struts>

Regards
James

James Strachan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
web: http://www.metastuff.com
mail: james@metastuff.com

  ----- Original Message ----- 
  From: James Strachan 
  To: struts-dev@jakarta.apache.org 
  Sent: 01 September 2000 17:35
  Subject: Patch: LinkTag for a single bean property


  I've just started using Struts and have just subscribed to this list so excuse me if this has been asked/answered before - I couldn't find a faq / archive of this list anywhere. (I'm trawling through the ezm lists which is a bit grim).

  I've attached a small patch to LinkTag.java which allows it to be used to construct a link when only one query argument is needed to be applied to a URL. I've found this to be particularly useful when using something like <struts:iterate>. 

  For example consider the following JSP:-

  <%
    Collection teams = ..;
    pageContext = userModel.getUserTeams( username );
    if ( teams != null && ! teams.isEmpty() ) {
      pageContext.setAttribute( "teams", teams );
  %>


    <struts:iterate id="bean" name="collectionOfBeans">
    <tr>    
      <td align="left"><struts:htmlProperty name="bean" property="id"/>    </td>
      <td align="left"><struts:htmlProperty name="bean" property="name"/></td>
      <td align="center">
        <struts:link href="editBean.do?action=Edit" id="bean_id" name="bean" property="id">
              <struts:message key="edit.bean"/>
        </struts:link>
      </td>

  It would generate the following hrefs in the table

      editBean.do?action=Edit&bean_id=1 
      editBean.do?action=Edit&bean_id=2 
      editBean.do?action=Edit&bean_id=3

  Note that the new 'id' tag attribute in the call of <struts:link> is optional to allow the property name to be redefined to the name of the query argument in cases where the property and URL parameter names differ.

  If there is a more elegant way of doing the above let me know - I must have missed it. Otherwise this makes the <struts:link> tag more flexible & reusable, as adding a single query argument is quite common. 

  Regards
  James

  James Strachan
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  web: http://www.metastuff.com
  mail: james@metastuff.com