2016-03-18 42 views
2

我想使用自定义taglib包含一个jsp文件。我已经完成了所有的taglib设置,但它显示了include语句而不是jsp内容。如何使用自定义taglib包含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(); 

我对taglibs真的很陌生,所以任何帮助表示赞赏。由于

回答

1

你可以的PageContext内设置与文件名称的属性,然后创建包括指令在JSP

像这样的事情

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

你的JSP:

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

然后在你的JSP中获取该属性和voula! 你并不真的需要添加或写入孔JSP文件到作家

希望它能帮助:)

+0

谢谢,我只是想它出。 – stillLearning

0

我想通了,使用的pageContext:

pageContext.include(ERROR_BLOCK_FILE_NAME); 
相关问题