struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tur...@apache.org
Subject cvs commit: jakarta-struts/src/share/org/apache/struts/taglib/html BaseHandlerTag.java
Date Sun, 05 Jan 2003 00:50:08 GMT
turner      2003/01/04 16:50:08

  Modified:    src/share/org/apache/struts/taglib/html BaseHandlerTag.java
  Log:
  Enable JSTL forEach and forToken support with indexed="true" HTML tags
  
  Revision  Changes    Path
  1.21      +64 -6     jakarta-struts/src/share/org/apache/struts/taglib/html/BaseHandlerTag.java
  
  Index: BaseHandlerTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/BaseHandlerTag.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- BaseHandlerTag.java	8 Dec 2002 06:54:50 -0000	1.20
  +++ BaseHandlerTag.java	5 Jan 2003 00:50:08 -0000	1.21
  @@ -65,11 +65,14 @@
   
   import javax.servlet.jsp.JspException;
   import javax.servlet.jsp.tagext.BodyTagSupport;
  +import javax.servlet.jsp.tagext.Tag;
   
   import org.apache.struts.Globals;
   import org.apache.struts.taglib.logic.IterateTag;
   import org.apache.struts.util.MessageResources;
   import org.apache.struts.util.RequestUtils;
  +import java.lang.reflect.Method;
  +import java.lang.reflect.InvocationTargetException;
   
   /**
    * Base class for tags that render form elements capable of including JavaScript
  @@ -78,6 +81,7 @@
    * appropriate implementations of these.
    *
    * @author Don Clasen
  + * @author James Turner
    * @version $Revision$ $Date$
    */
   
  @@ -570,6 +574,46 @@
   
       }
   
  +    private Class loopTagSupportClass = null;
  +    private Method loopTagSupportGetStatus = null;
  +    private Class loopTagStatusClass = null;
  +    private Method loopTagStatusGetIndex = null;
  +    private boolean triedJstlInit = false;
  +    private boolean triedJstlSuccess = false;
  +
  +    private Integer getJstlLoopIndex () {
  +	if (!triedJstlInit) {
  +	    triedJstlInit = true;
  +	    try {
  +		loopTagSupportClass = 
  +		    Class.forName("javax.servlet.jsp.jstl.core.LoopTagSupport");
  +		loopTagSupportGetStatus = 
  +		    loopTagSupportClass.getDeclaredMethod("getLoopStatus", null);
  +		loopTagStatusClass =
  +		    Class.forName("javax.servlet.jsp.jstl.core.LoopTagStatus");
  +		loopTagStatusGetIndex = 
  +		    loopTagStatusClass.getDeclaredMethod("getIndex", null);
  +		triedJstlSuccess = true;
  +	    }
  +	    catch (ClassNotFoundException ex) {}
  +	    catch (NoSuchMethodException ex) {}
  +	}
  +	if (triedJstlSuccess) {
  +	    try {
  +		Object loopTag = findAncestorWithClass(this, loopTagSupportClass);
  +		if (loopTag == null)  return null;
  +		Object status = loopTagSupportGetStatus.invoke(loopTag, null);
  +		return (Integer) loopTagStatusGetIndex.invoke(status, null);
  +	    } 
  +	    catch (IllegalAccessException ex) {}
  +	    catch (IllegalArgumentException ex) {}
  +	    catch (InvocationTargetException ex) {}
  +	    catch (NullPointerException ex) {}
  +	    catch (ExceptionInInitializerError ex) {}
  +	}
  +	return null;
  +    }
  +
       /**
        *  Appends bean name with index in brackets for tags with
        *  'true' value in 'indexed' attribute.
  @@ -577,9 +621,23 @@
        *  @exception JspException if 'indexed' tag used outside of iterate tag.
        */
       protected void prepareIndex(StringBuffer handlers, String name) throws JspException
{
  +	int index = 0;
  +	boolean found = false;
  +
           // look for outer iterate tag
           IterateTag iterateTag = (IterateTag) findAncestorWithClass(this, IterateTag.class);
  -        if (iterateTag == null) {
  +	// Look for JSTL loops
  +	if (iterateTag == null) {
  +	    Integer i = getJstlLoopIndex();
  +	    if (i != null) {
  +		index = i.intValue();
  +		found = true;
  +	    }
  +	} else {
  +	    index = iterateTag.getIndex();
  +	    found = true;
  +	}
  +        if (!found) {
               // this tag should only be nested in iteratetag, if it's not, throw exception
               JspException e = new JspException(messages.getMessage("indexed.noEnclosingIterate"));
               RequestUtils.saveException(pageContext, e);
  @@ -588,7 +646,7 @@
           if (name != null)
               handlers.append(name);
           handlers.append("[");
  -        handlers.append(iterateTag.getIndex());
  +        handlers.append(index);
           handlers.append("]");
           if (name != null)
               handlers.append(".");
  
  
  

--
To unsubscribe, e-mail:   <mailto:struts-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-dev-help@jakarta.apache.org>


Mime
View raw message