简体   繁体   中英

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

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 :)

我想出了使用 pageContext :

 pageContext.include(ERROR_BLOCK_FILE_NAME);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM