This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return static text in JSP Tag Libraries???

 
tuancan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just have started in studying JSP Tag Libraries. I write a tag lib just return static content's body of tag to user. I tried to write as below:
package com.tagLibs;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class readBodyOfTag extends BodyTagSupport {
public int doAfterBody() throws JspException {
return EVAL_BODY_INCLUDE;
}
}
But it could not run, compiling is OK. It didn't show any error.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
When I tried as below:
package com.tagLibs;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class readBodyOfTag extends BodyTagSupport {
public int doAfterBody() throws JspException {
try {
BodyContent body = getBodyContent();
JspWriter writer = body.getEnclosingWriter();
String bodyString = body.getString();
if (bodyString != null) {
writer.println(bodyString);
}
}
catch (IOException ioe) {
throw new JspTagException("Error: IOException while writing to the user!");
}
return SKIP_BODY;
}
}
It works well. But I don't know why I must write a lot of code just for return static content's body of tag to user.
Can anybody tell me why the first code can not run? Thx a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic