开发者

Custom Tag implementation issue

开发者 https://www.devze.com 2022-12-25 08:59 出处:网络
I have customs tags as follows. repeat and heading tag have doAfterBody method implemented.Both of them extends BodyTagSupport class

I have customs tags as follows. repeat and heading tag have doAfterBody method implemented.Both of them extends BodyTagSupport class

        <csajsp:repeat reps="5">
                <LI>
                    <csajsp:heading bgColor="BLACK">
                        White on Black Heading
                    </csajsp:heading>
                </LI>
            </csajsp:repeat>

Repeat tag Class

    public void setReps(String repeats) {
              System.out.println("TESTING"+repeats);
                       //sets the reps variable.
    }
    public int doAfterBody() {
              System.out.println("Inside repeate tag"+reps);
            if (reps-- >= 1) {
              BodyContent body = getBodyContent();
              try {
                JspWriter out = body.getEnclosingWriter();
                System.out.println("BODY"+body.getString());
                out.println(body.getString());
                body.clearBody(); // Clear for next evaluation
              } catch(IOException ioe) {
                System.out.println("Error in RepeatTag: " + ioe);
              }
              return(EVAL_BODY_TAG);
            } else {
              return(SKIP_BODY);
            }
          }

Class of Heading tag

      public int doAfterBody()
          {
              System.out.println("inside heading tag");
              BodyContent body = getBodyContent();
              System.out.println(body.getString());
                try {
                  JspWriter out = body.getEnclosingWriter();
                  out.print("NEW TEXT");
                } catch(IOException ioe) {
                  System.out.println("Error in FilterTag: " + ioe);
                }
                // SKIP_BODY means I'm done. If I wanted to evaluate
                // and handle the body again, I'd return EVAL_BODY_TAG.
           开发者_开发百科     return(SKIP_BODY);
          }
          public int doEndTag() {
                try {
                  JspWriter out = pageContext.getOut();
                  out.print("NEW TEXT 2");
                } catch(IOException ioe) {
                  System.out.println("Error in HeadingTag: " + ioe);
                }
                return(EVAL_PAGE); // Continue with rest of JSP page
              }

Custom tag tld file is

<taglib>
  <tlibversion>1.0</tlibversion>
   <jspversion>1.1</jspversion>
  <shortname>csajsp</shortname>
  <uri></uri>
  <tag>
    <name>heading</name>
    <tagclass>com.test.tags.HeadingTag</tagclass>
    <bodycontent>JSP</bodycontent>
     <attribute>
      <name>bgColor</name>
      <required>true</required> <!-- bgColor is required -->
    </attribute>
  </tag>
   <tag>
    <name>repeat</name>
    <tagclass>com.test.tags.RepeatTag</tagclass>
    <info>Repeats body the specified number of times.</info>
    <bodycontent>JSP</bodycontent>
    <attribute>
      <name>reps</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
</taglib>

The order in which SOP are printed is

  1. Setter method of csajsp:repeat is called.
  2. White on Black Heading is printed. ie doAfterBody of csajsp:heading tag is called.

I don't know why it is not calling doAfterBody of csajsp:repeat tag.

Please help me to understand this.


What class do your tags extend?

The default behaviour of TagSupport is to return SKIP_BODY, which would skip processing of the tag body.

The default behaviour of BodyTagSupport is to return EVAL_BODY_BUFFERED, which would process the tag body.

If you are implementing BodyTag yourself, then you need to make sure you override doStartTag correctly to indicate that the JSP body should be evaluated.

0

精彩评论

暂无评论...
验证码 换一张
取 消