Search code examples
javajspcustom-tagsjsp-fragments

Include jspf file with custom tag


How can I add jspf file to jsp page via custom tag?

In the tag support class I can add code of jspf using....

JspWriter writter = this.pageContext.getOut();
writter.append( "html code");

But what shall I do to add file?


Solution

  • If by "jspf file" you mean a JSP fragment that you reference in a static include (eg, <%@ include file="fragment.jspf" %>), you can't.

    The reason is that static includes are processed before the JSP is compiled, while tag libraries are processed afterward.

    If, instead, you mean a dynamic include (normally invoked via <jsp:include>), then you can do this from a tag handler via the request dispatcher.

    You could always wrap the static include in a tag handler that implements some conditional logic. This would control whether the output of that fragment is inserted into the rendered page. However, it would still be compiled into the page, and I suspect that your goal is to reduce the overall size of your JSPs, in which case you're out of luck.