Link to home
Start Free TrialLog in
Avatar of tekel
tekel

asked on

iterate tag doesn't evaluate HTML

hi all,

I created my own iterate tag.  the problem is that it recognizes the nested tags within the body of the iterate tag, but not the HTML.

here is what I mean. say I have the following :

<mytag:iterate>
     <mytag:item property="name"/> <p>
     <mytag:item property="number"/> <p>
     <hr>
</mytag:iterate>

the "name" and the "number" of the items are printed okay, but the <p> and the <hr> are never seen.  i.e the "name" and "number" are bunched up together and not separated.

on my tld I have:
<name>iterate</name>
     <tagclass>com.util.IterateTag</tagclass>
<bodycontent>JSP</bodycontent>

I've set the bodycontent tag to JSP which I assumed would evaluate the HTML.


in my Iterate tag I have:
public int doAfterBody() throws JspException {
try {
     BodyContent bc = getBodyContent();
     getPreviousOut().print(bc.getString());
     bc.clearBody();
     ...
}
I'm not sure what the above code does. I got this from an example. could this be causing the problem?

thanks,
- tekel
Avatar of jerch
jerch

Try this

public int doAfterBody() {
     if (keepGoing) { // whatever condition you have... you can change the condition
          BodyContent body = getBodyContent();
          try {
               JspWriter out = body.getEnclosingWriter();
               out.println(body.getString());
               body.clearBody();
          } catch (IOException e) {
          }
          return EVAL_BODY_TAG; // or EVAL_BODY_AGAIN if you're using JSP 1.2
     } else {
          return SKIP_BODY;
     }
}

or simply replace the getPreviousOut() with body.getEnclosingWriter()

best regards,
Jerson
Hi.

As far as I can see, your iteration tag does nothing but iterate the contained body a number of times, right? if so, please try this:

public class com.util.IterateTag extends TagSupport implements Iterationtag {
     public int doAfterBody() throws JspTagException {
          if (want_one_more_iteration) {
               return EVAL_BODY_AGAIN;
          }
          else {
               return SKIP_BODY;
          }
     }
}

The text one_more_iteration is a placeholder for a condition that evaluates to true if you want to perform one more iteration of the body.

The evaluation of the body is left to the JSP engine, you do not have to worry about this.

Since your tag does not write any html code by itself (like an enclosing table or font), you do not need to implement the doStartTag and doEndTag methods.


If this doesn't work, please post the complete code of your tag an state which JSP container and version you are using. There has been a major change concerning the iteration tag in JSP 1.2.

HTH,
Peter
Hi tekel, :-)

public int doAfterBody() throws JspException
{
     if (checkIfThereAreMoreIterations())
     {
          signalDataForCurrentIteration();
          return EVAL_BODY_TAG;
     }
     else
     {
          try
          {
               bodyContent.writeOut( bodyContent.getEnclosingWriter() );
          }
          catch( IOException e )
          {
               throw new JspException( "I/O Exception :"+e.getMessage() );
          }
          return SKIP_BODY;
     }
}

Greetings,
    </ntr> :)
...of course, my example implies that your Tag should extend BodyTagSupport.

Greetings,
    </ntr> :)
Avatar of tekel

ASKER

Hi all,

thanks very much for the comments.  I am using JSP version 1.1 on Weblogic 5.1


I can't seem to get it to work with the following code above.  I've listed the code above. thanks for bearing with the long post while I solve this issue.


I have SimpleIterateTag, SimpleItem tag and my JSP file. SimpleIterateTag iterates 2 times.  SimpleItem tag displays some String value.

my JSP looks like:

                         <auction:simpleIterate>
                              iterate<br>iterate<p>
                              <auction:simpleItem/>                               <br>
                              <auction:simpleItem/>
                         </auction:simpleIterate>

== source code of jsp result is ===
itemitem
     iterate<br>iterate<p>
     
     <br>

     

     iterate<br>iterate<p>
     
     <br>
     
itemitem
     iterate<br>iterate<p>
     
     <br>
     

     iterate<br>iterate<p>
     
     <br>

     
itemitem
     iterate<br>iterate<p>
     
     <br>
     

     iterate<br>iterate<p>
     
     <br>

=== start of SimpleIterateTag ===
public class SimpleIterateTag extends BodyTagSupport {

     private int count = 0;

     public int doStartTag() throws JspException {
          return EVAL_BODY_TAG;
     }
     public int doAfterBody() throws JspException {
          try {
               BodyContent bc = getBodyContent();
               getPreviousOut().print(bc.getString());

               pageContext.getResponse().getWriter().print(bc.getString());
               bc.clearBody();
          }
          catch (IOException e) {}


          if(++count <= 2) {
               return EVAL_BODY_TAG;
          } else {
               return SKIP_BODY;
          }

     }
     /**
      * Releases all instance variables.
      */
     public void release() {
          super.release();
     }
}

=== end of SimpleIterateTag ===


=== start of SimpleItem ======

public class SimpleItemTag extends TagSupport {
     public int doStartTag() throws JspTagException {
          try {
               pageContext.getResponse().getWriter().print("item");
          } catch (java.io.IOException ioe) {
               throw new JspTagException("io exception: " + ioe);
          }
          return EVAL_BODY_INCLUDE;
     }
}

=== end SimpleItem
Hi tekel,
As I've said in my previous post,
In SimpleIterateTag, just replace these two lines of code

getPreviousOut().print(bc.getString());
pageContext.getResponse().getWriter().print(bc.getString());

to

JspWriter out = bc.getEnclosingWriter();
out.println(bc.getString());

and on SimpleItemTag
Replace this
pageContext.getResponse().getWriter().print("item");

to

JspWriter out = pageContext.getOut();
out.println("item");

hope this helps.
Jerson
Avatar of tekel

ASKER

Hi all,

thanks very much for the comments.  I am using JSP version 1.1 on Weblogic 5.1


I can't seem to get it to work with the following code above.  I've listed the code above. thanks for bearing with the long post while I solve this issue.


I have SimpleIterateTag, SimpleItem tag and my JSP file. SimpleIterateTag iterates 2 times.  SimpleItem tag displays some String value.

my JSP looks like:

                         <auction:simpleIterate>
                              iterate<br>iterate<p>
                              <auction:simpleItem/>                               <br>
                              <auction:simpleItem/>
                         </auction:simpleIterate>

== source code of jsp result is ===
itemitem
     iterate<br>iterate<p>
     
     <br>

     

     iterate<br>iterate<p>
     
     <br>
     
itemitem
     iterate<br>iterate<p>
     
     <br>
     

     iterate<br>iterate<p>
     
     <br>

     
itemitem
     iterate<br>iterate<p>
     
     <br>
     

     iterate<br>iterate<p>
     
     <br>

=== start of SimpleIterateTag ===
public class SimpleIterateTag extends BodyTagSupport {

     private int count = 0;

     public int doStartTag() throws JspException {
          return EVAL_BODY_TAG;
     }
     public int doAfterBody() throws JspException {
          try {
               BodyContent bc = getBodyContent();
               getPreviousOut().print(bc.getString());

               pageContext.getResponse().getWriter().print(bc.getString());
               bc.clearBody();
          }
          catch (IOException e) {}


          if(++count <= 2) {
               return EVAL_BODY_TAG;
          } else {
               return SKIP_BODY;
          }

     }
     /**
      * Releases all instance variables.
      */
     public void release() {
          super.release();
     }
}

=== end of SimpleIterateTag ===


=== start of SimpleItem ======

public class SimpleItemTag extends TagSupport {
     public int doStartTag() throws JspTagException {
          try {
               pageContext.getResponse().getWriter().print("item");
          } catch (java.io.IOException ioe) {
               throw new JspTagException("io exception: " + ioe);
          }
          return EVAL_BODY_INCLUDE;
     }
}

=== end SimpleItem
Avatar of tekel

ASKER

Hi jerch,

Great job! the point is yours.  I've increased the points since I still have one question for you.  Can you tell me why i was getting undesired results when I was using :

getPreviousOut().print(bc.getString());
pageContext.getResponse().getWriter().print(bc.getString());

instead of :

JspWriter out = bc.getEnclosingWriter();
out.println(bc.getString());

I would have thought that they did the same thing - and that is to print out the string to the page.

- tekel,
ASKER CERTIFIED SOLUTION
Avatar of jerch
jerch

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tekel

ASKER

thanks jerch,

you've been a big help.

- tekel
sure.... no problem :)