tags:

views:

300

answers:

3

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.

A: 

what you really need to have is this:

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

and change your doTag to something like this

@Override public void doTag() throws JspException, IOException {
try {
   BodyContent bc = getBodyContent();
   String body = bc.getString();
   // do something to the body here.
   JspWriter out = bc.getEnclosingWriter();
   if(body != null) {
     out.print(buff.toString());
   }
 } catch(IOException ioe) {
   throw new JspException("Error: "+ioe.getMessage());
 }
}

make sure the jsp body content is set to jsp in the tld:

<bodycontent>JSP</bodycontent>
mkoryak
Nesting the taglib directly in the JSP is not an option, since I'll have it dynamically generated from a third party library for instance.
Tiago Fernandez
A: 

Why do you write a JSTL tag inside your doTag method? The println is directly going into the compiled JSP (read: servlet) When this gets rendered in the browser it will be printed as it is since teh browser doesn't understand JSTL tags.

public class MyTaglib extends SimpleTagSupport {
      @Override public void doTag() throws JspException, IOException {
        getJspContext().getOut().println("My enclosed tag");
        getJspBody().invoke(null);
      }
    }

You can optionally add HTML tags to the string.

Vaishak Suresh
Because such JSTL tag would come from a third party template evaluation.
Tiago Fernandez
+1  A: 

Hello, all!

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

brandizzi
I haven't really tested this because at some point the requirements have changed and I didn't need to solve this particular problem anymore, so I'm accepting your solution since it was the best answer and you tested it yourself. Thanks :)
Tiago Fernandez