• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending BodyTagSupport (HFS CH10 Q9)

 
Dee Brown
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain why answer 9C (chapter 10) is not true (p594). The provided 'reason' is that returning EVAL_BODY_BUFFERED directs the body to a buffer which "this" tag does not process. However, the state diagram on p563 reveals the body is evaluated after doInitBody(). Furthermore, page 2-72 of the 2.1 spec states, "[BodyContent] will be passed to doInitBody(); then the body of the tag will be evaluated".

This is an area that was glossed over in the book. However, from the information provided I assume you override either setBodyCOntent or doInitBody if manipulation of the BodyContent is required. And if you fail to override either of those methods, returning EVAL_BODY_BUFFERED produces the results that would have been obtained if EVAL_BODY_INCLUDE had been returned. If this is correct, 9C (return EVAL_BODY_BUFFERED) would also be true.
 
Dee Brown
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll just write the code, which is what I have been doing for many of the issues I have turned up. As a result, no response necessary.
 
Dee Brown
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As indicated in my original post, when extending BodyTagSupport, if setBodyContent and doInitBody are not overridden, returning EVAL_BODY_INCLUDE produces the same rendered result as returning EVAL_BODY_BUFFERED. As a result, chapter 10 Q9 (pg 594) should have B and C as correct answers. Code provided below:

testJSP.jsp:
<%@ taglib prefix="mytags" uri="BodyTagTesting" %>
<html><body>

<mytag:tagTest>BodyContent</mytag:tagTest>

</body></html>

testJSP2.jsp:
<%@ taglib prefix="mytags" uri="BodyTagTesting" %>
<html><body>

<mytag:tagTest2>BodyContent2</mytag:tagTest2>

</body></html>

WEB-INF/BufTag.java:
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspException;

public class BufTag extends BodyTagSupport {
public int doStartTag() throws JspException {
return EVAL_BODY_INCLUDE;
}
}

WEB-INF/BufTag2.java:
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspException;

public class BufTag2 extends BodyTagSupport {
public int doStartTag() throws JspException {
return EVAL_BODY_BUFFERED;
}
}

WEB-INF/bufTest.tld:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising BodyTagSupport functionality.</description>
<tlib-version>1.0</tlib-version>
<short-name>BodyTagTesting</short-name>
<uri>BodyTagTesting</uri>

<tag>
<name>tagTest</name>
<tag-class>BufTag</tag-class>
<body-content>tagdependent</body-content>
</tag>
<tag>
<name>tagTest2</name>
<tag-class>BufTag2</tag-class>
<body-content>tagdependent</body-content>
</tag>
</taglib>
 
Pham Hoai Van
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didn't get what you have. how comes?
here is my code:

and jsp:

when doStartTag return EVAL_BODY_INCLUDE i got the body content, but when return EVAL_BODY_BUFFERED i got nothing. exactly as HF's answer.
Further more, in case returning EVAL_BODY_BUFFERED where did the body content goes? i tried to log the bodyContent out but nothing there.

Any idea?
 
gopal venu
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for getting the body two ways

1.BodyTagSupport.getPriviousOut()
2.PageContext.popBody();

Try it..make sure use them in proper life cycle method..doEndTag() is fine one for both cases...

Let me correct if i m wrong
 
Dee Brown
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said in my original post, "if you fail to override either of those methods, returning EVAL_BODY_BUFFERED produces the results that would have been obtained if EVAL_BODY_INCLUDE had been returned." I could be wrong. However, since I provided the code that met the above requirement, to confirm or refute my stated results it would have been simpler just to run my code. You have provided different code and you indicate it produces different results, which does not address the issue completely.
 
Dee Brown
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will just add that as indicated in my original post, info in this area is lacking. If I did override the methods in question, I would also consider invoking the superclass methods (even though "no action" is indicated for doInitBody()). However, I was considering functionality that is present when the methods in question were not implemented, which I believe is addressed in the code I provided.
 
Dee Brown
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, see the following post for an independent assessment of "return EVAL_BODY_BUFFERED" results:

Doubt Enthuware Mock Exam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic