繁体   English   中英

如何从自定义 jsp 标记中包含另一个 jsp 页面

[英]How to include another jsp page from custom jsp tag

I want to have a custom Jsp tag with one string attribute, if this is aa path to another jsp file then I want to include that JSP in the page (behave as a jsp:include); 如果没有,那么我只想 output 页面正文中的字符串。

我不知道怎么做 jsp:include 部分...

我看到 SimpleTagSupport 中有一个 setJspBody 可以传递一个 JspFragment ......但我不知道如何得到它......我希望在 javax.servlet.jsp 中找到类似 IncludeTag class 的东西,我可以委托这个逻辑就像setJspBody( new IncludeTag(...).getJspFragment() )但我没有找到 class 在文档中实现 jsp:include 功能的内容。

我使用了 PageContext.include

public class MyIncludeTag extends TagSupport {
   private String page;
   //getters setters
   @Override
   public int doStartTag() throws JspException {
     try {
            pageContext.include(page);
        } catch (ServletException | IOException e) {
            throw new JspException("Failed to include " + page, e);
        }
        return super.doStartTag();
   }
}

暂无
暂无

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

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