JSP Custom Taglib:嵌套评估

Tia*_*dez 6 java jsp taglib

说我有我的自定义taglib:

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

<mytaglib:doSomething>
  Test
</mytaglib:doSomething>
Run Code Online (Sandbox Code Playgroud)

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

public class MyTaglib extends SimpleTagSupport {

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

我的输出是:

<c:out value="My enclosed tag"/>
Test
Run Code Online (Sandbox Code Playgroud)

当我真的需要输出这个:

My enclosed tag
Test
Run Code Online (Sandbox Code Playgroud)

这可行吗?怎么样?

谢谢.

bra*_*zzi 2

Tiago,我不知道如何解决您的具体问题,但您可以解释文件中的 JSP 代码。只需创建一个 RequestDispatcher 并包含 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();
}
Run Code Online (Sandbox Code Playgroud)

我在 Liferay portlet 中测试了这段代码,但我相信无论如何它也应该在其他上下文中工作。如果没有,我想知道:)

华泰