【问题标题】:How to include another jsp page from custom jsp tag如何从自定义 jsp 标签中包含另一个 jsp 页面
【发布时间】:2021-06-15 01:02:39
【问题描述】:

我想要一个带有一个字符串属性的自定义 Jsp 标记,如果这是另一个 jsp 文件的路径,那么我想在页面中包含该 JSP(表现为 jsp:include);如果没有,那么我只想在页面正文中输出字符串。

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

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

【问题讨论】:

    标签: java jsp jstl java-ee-5


    【解决方案1】:

    我用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();
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-06
      • 2014-11-16
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      相关资源
      最近更新 更多