简体   繁体   English

如何使用自定义 taglib 包含一个 jsp 文件?

[英]How to include a jsp file using a custom taglib?

I am trying to include a jsp file using a custom taglib.我正在尝试使用自定义 taglib 包含一个 jsp 文件。 I have all the taglib setup in place, but it shows the include statement instead of the jsp content.我有所有的 taglib 设置,但它显示了 include 语句而不是 jsp 内容。

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.我对 taglibs 真的很陌生,所以感谢任何帮助。 Thanks谢谢

You could set an attribute with the name of the file within PageContext and then create your include directive in the JSP您可以使用PageContext中的文件名设置一个属性,然后在 JSP 中创建包含指令

Something like this像这样的东西

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

Your JSP:您的 JSP:

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

and then in your JSP, get that attribute and voula!然后在您的 JSP 中,获取该属性和 voula! You don't really need to add or write the hole JSP file into the writer你真的不需要在编写器中添加或写入孔 JSP 文件

Hope it helps :)希望能帮助到你 :)

我想出了使用 pageContext :

 pageContext.include(ERROR_BLOCK_FILE_NAME);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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