JSP Custom Taglib: Nested Evaluation

java, jsp, taglib

Solution

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

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

Problem

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> ``` 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); } } ``` The output I have is: ``` <c:out value="My enclosed tag"/> Test ``` When I actually need to output this: ``` My enclosed tag Test ``` Is this feasible? How? Thanks.

Original source