1

Every example i can find has the tag handler java class generating html and spewing it out with out.print(someHTML);

Is there a way to include a jsp and add attributes to the request instead?

blank
  • 17,852
  • 20
  • 105
  • 159

2 Answers2

1

I haven't tried this but it should be possible by obtaining a RequestDispatcher from the Request object:

public int doStartTag() throws JspException {
    try {
        pageContext.setAttribute("title", "My Title");
        pageContext.getRequest().getRequestDispatcher("/WEB-INF/includes/header.jspf").include(pageContext.getRequest(), pageContext.getResponse());
    }
    catch (IOException e) {

    }
    return EVAL_BODY_INCLUDE;
}

The PageContext also has an include method but that seems to only work for static files, not jsps.

Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • Just an update for this - setting attributes on pageContext doesn't work you need to do it on the request scope & it's necessary to do a .flush() before using the request dispatcher or the output doesn't get included int he right place. Mind if I update your answer? – blank Apr 05 '12 at 07:59
  • @BedwyrHumphreys: Go ahead, thats the spirit of Stack Overflow. – Jörn Horstmann Apr 05 '12 at 10:30
1

Try a JSP custom tag file; here's a simple example using an attribute.

Tag files have to live under WEB-INF/tags, so in WEB-INF/tags/makebold.tag:

<%@ attribute name="toBold" required="true" %>

<b>${toBold}</b>

In boldtest.jsp:

<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>

<my:makebold toBold="this will be bolded" />

I read up on tag files here and here.

Mike Partridge
  • 5,128
  • 8
  • 35
  • 47