You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@jakarta.apache.org by Lothar Behrens <lb...@GIquadrat.de> on 2000/01/28 18:43:20 UTC

Tag problem


Hello,

I created a for - tag and - in general it would work.
But the enclosed code in the for tag is not processed like in the outside of
this tag.

What must I do, that the code is evaluated and the loop variable is printed like
this output?

Do something with 0.
Do something with 1.
Do something with 2.
Do something with 3.
and so forth...

Here is the code:

/**
 * A for tag. Similary to for (int i = 0; i < 0; i++) { ... }
 *
 * <!-- Usage: -->
 * <%
 * pageContext.setAttribure("start", new Integer(0));
 * pageContext.setAttribure("end", new Integer(10));
 *
 * <for loop="loop" start="start" end="end">
 * Do something with <%= loop %>.
 * </for>
 */

public class ForTag extends BodyTagSupport
{
     public ForTag() {
          super();
     }

     /**
      * Attributes not copied in here, because this works.
      */


    public int doStartTag() throws JspTagException
    {
     loop = new Integer(0);

     loopStart = (Integer) pageContext.getAttribute(startId);
     loopEnd = (Integer) pageContext.getAttribute(endId);

     loop = loopStart;

     if (!loop.equals(loopEnd)) {
          pageContext.setAttribute(loopId, loop);
          loop = new Integer(loop.intValue() + 1);
          return EVAL_BODY_TAG;
     } else {
          return SKIP_BODY;
     }
    }

    public int doAfterBody() throws JspTagException {
          if (!loop.equals(loopEnd)) {
               pageContext.setAttribute(loopId, loop);
               loop = new Integer(loop.intValue() + 1);
               return EVAL_BODY_TAG;
          } else {
               return SKIP_BODY;
          }
    }

    public int doEndTag() {
     return EVAL_PAGE;
    }

    public void release() {
    loop = null;
     startId = null;
     endId = null;
     super.release();
    }
     private Integer loop;    private Integer loopStart;     private Integer
loopEnd;
     private String startId;  private String endId;          private String
loopId;
}



Re: Tag problem

Posted by Volker Turau <tu...@cad01.informatik.fh-wiesbaden.de>.
Hi Lothar,

Your code is rather complicated, if I understand you correctly you
have to do the following:
-- define a class ForTag
-- define a class ForTEI (introducing your variable "loop")
-- extend your taglib descriptor

Below you find the code for this!!
Look out for my forthcoming book on Java Server Pages with lots of
examples on user defined tags.

have fun!!!

volker turau
FH Wiesbaden Fachbereich Informatik
Tel.: +49-611-9495-205 FAX +49-611-9495-210
http://www.informatik.fh-wiesbaden.de/~turau


%%%%%%%% class ForTag
public class ForTag extends BodyTagSupport {

  private int start = 0, end = 0, current;
  private String loop;

  public void setStart(int s) {
    start = s;
  }

  public void setEnd(int e) {
    end = e;
  }

  public void setLoop(String l) {
    loop = l;
  }
    
  public int doStartTag() throws JspException {
    current = start;
    if (current > end)
       return SKIP_BODY;
    pageContext.setAttribute(loop, new Integer(current));
    return EVAL_BODY_TAG;
  }
  
  public int doAfterBody() throws JspException {
    BodyContent body = getBodyContent();
    try {
      body.writeOut(getPreviousOut());
    } catch (IOException ex) {
      throw new JspError("unexpected IO error");
    }
    body.clearBody();
    current++;
    pageContext.setAttribute(loop, new Integer(current));
    return (current <= end) ? EVAL_BODY_TAG : SKIP_BODY;
  }
}


%%%%%%%% class ForTEI
package taglib;

import javax.servlet.jsp.tagext.*;

public class ForTEI extends TagExtraInfo {

  public VariableInfo[] getVariableInfo(TagData data) {
    String rowValue = data.getAttributeString("loop");
    
    if (rowValue == null) {
      throw new Error("TagLibrary inconsistency");
    }
    
    VariableInfo info
      = new VariableInfo(rowValue,
			 "java.lang.Integer",
			 true,
			 VariableInfo.NESTED);
    VariableInfo[] back = { info } ;
    return back;
  }

  public boolean isValid(TagData data) {
    return true;
  }
}

%%%%%%%% tag descriptor
  <tag>
    <name>for</name>
    <tagclass>taglib.ForTag</tagclass>
    <teiclass>taglib.ForTEI</teiclass>
    <bodycontent>JSP</bodycontent>
    <info>
	A very simple iteration tag.
    </info>

    <attribute>
	<name>loop</name>
	<required>true</required>
	<rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
	<name>start</name>
	<required>true</required>
	<rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
	<name>end</name>
	<required>true</required>
	<rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>


%%%%%%%%%% Usage:
<%@ page session="false" %>
<%@ taglib uri="fhw.jar" prefix="fhw" %>

<html>
<body>
<h3 align=center>Das Tag fhw:for</h3>


<p>Syntax:<br>
 <code>&lt;fhw:for loop="name" start="wert" end="wert"&gt;</code>
....
<code>&lt;fhw:for/&gt;</code>
<p>
Example:<br>
<code>&lt;fhw:for loop="name" start="0" end="5"&gt;</code><br>
Do something with &lt;%= loop %&gt;&lt;br&gt;<br>
<code>&lt;fhw:for/&gt;</code>
<p>

Result:
<p>
<fhw:for loop="loop" start="0" end="5">
 Do something with <%= loop %><br>
</fhw:for>

hhh
</body>
</html>