• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TagSupport

 
Sharma Anjali
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HF Page 766 Ques 12 Final Mock Exam

Ques12

Given this tag handler

package com;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class WorthLessTag extends TagSupport {

private String x;
public void setX(String x) { this.x = x; }
public int doStartTag() throws JspException {
try {
pageContext.getOut().print(x);
}
catch (IOException e) {}
if("x".equals(x))
return SKIP_BODY;
else
return EVAL_BODY_INCLUDE;

}

public int doEndTag() throws JspException {
try {
pageContext.getOut().print("E");
}
catch (IOException e) {}

if("y".equals(x))
return SKIP_PAGE;
else
return EVAL_PAGE;

}
}


and given this TLD
<tag>
<name>worthless</name>
<tag-class>com.WorthLessTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>x</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>


The tld clearly mentions that body-content should be empty but the doStartTag returns EVAL_BODY_INCLUDE.
Why doesn't this give an error ?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... because it's allowed. Just like you can have tags that support JSP content, but tell your app that no JSPs can have JSP content without suddenly making everything not compile ... you can do the same thing here in your example. Basically, think of the tag as the object, and the TLD as the context for the tag -- two separate things.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic