Shweta
Shweta

Reputation: 922

How to include a jsp file using a custom taglib?

I am trying to include a jsp file using a custom taglib. I have all the taglib setup in place, but it shows the include statement instead of the jsp content.

public int doStartTag() throws JspException {
    JspWriter out = pageContext.getOut();
    StringBuffer sb = new StringBuffer();

    if (section != null && !section.isEmpty()) {
        sb.append("<%@ include file=\"defaultHeader.jsp\" %>");
    }
    try {
        out.println(sb.toString());
    } catch (IOException e) {
        log.error("Failed to generate the tag", e);
        e.printStackTrace();
    }
    return super.doStartTag();

I am really new to taglibs, so any help is appreciated. Thanks

Upvotes: 2

Views: 845

Answers (2)

Shweta
Shweta

Reputation: 922

I figured out using the pageContext :

 pageContext.include(ERROR_BLOCK_FILE_NAME);

Upvotes: 0

AlbertoRuvel
AlbertoRuvel

Reputation: 356

You could set an attribute with the name of the file within PageContext and then create your include directive in the JSP

Something like this

pageContext.setAttribute("includeFileName", "YourFile.jsp path", PageContext.REQUEST_SCOPE); 

Your JSP:

<%@include file="${pageContext.includeFileName}" %>

and then in your JSP, get that attribute and voula! You don't really need to add or write the hole JSP file into the writer

Hope it helps :)

Upvotes: 1

Related Questions