JSP Custom Taglib:嵌套评估 [英] JSP Custom Taglib: Nested Evaluation

查看:130
本文介绍了JSP Custom Taglib:嵌套评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有自定义taglib:

Say I have my custom taglib:

<%@ taglib uri="http://foo.bar/mytaglib" prefix="mytaglib"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<mytaglib:doSomething>
  Test
</mytaglib:doSomething>

在taglib类中,我需要处理模板并告诉JSP重新评估其输出,所以例如,如果我有这个:

Inside the taglib class I need to process a template and tell the JSP to re-evaluate its output, so for example if I have this:

public class MyTaglib extends SimpleTagSupport {

  @Override public void doTag() throws JspException, IOException {
    getJspContext().getOut().println("<c:out value=\"My enclosed tag\"/>");
    getJspBody().invoke(null);
  }
}

我的输出是:

<c:out value="My enclosed tag"/>
Test

当我实际需要输出时:

My enclosed tag
Test

这可行吗?怎么样?

谢谢。

推荐答案

Tiago,我不知道如何解决完全问题,但您可以从文件中解释JSP代码。只需创建一个RequestDispatcher并包含JSP:

Tiago, I do not know how to solve your exact problem but you can interpret the JSP code from a file. Just create a RequestDispatcher and include the JSP:

    public int doStartTag() throws JspException {
    ServletRequest request = pageContext.getRequest();
    ServletResponse response = pageContext.getResponse();

    RequestDispatcher disp = request.getRequestDispatcher("/test.jsp");
    try {
        disp.include(request, response);
    } catch (ServletException e) {
        throw new JspException(e);
    } catch (IOException e) {
        throw new JspException(e);
    }
    return super.doStartTag();
}

我在Liferay portlet中测试了这段代码,但我相信它应该适用于无论如何其他情况。如果没有,我想知道:)

I tested this code in a Liferay portlet, but I believe it should work in other contexts anyway. If it don't, I would like to know :)

HTH

这篇关于JSP Custom Taglib:嵌套评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆