tags:

views:

333

answers:

2

I want to use custom jsp tags to help build a menu in my application. However, I want all the actual HTML to live in JSP files, rather than in the Java class.

So, suppose I have a tag like this:

<mytags:Menu>
  <mytags:MenuItem name="foo"/>
  <mytags:MenuItem name="bar"/>
  <mytags:MenuItem name="baz"/>
</mytags:Menu>

I then might have the class for my Menu tag, that looks like this:

public class MenuPill extends TagSupport {
    public int doStartTag() throws JspException {
     try {
      pageContext.include("/menu/menu.jsp");
     } catch (ServletException e) {
      throw new JspException(e);
     } catch (IOException e) {
      throw new JspException(e);
     }
     return super.doStartTag();
    }
}

My menu.jsp file, which is the wrapper for the menu itself, then might look like this:

<div id="menu>
  <%somehow include the body here%>
</div>

What I want to do is put the body of my mytags:Menu tag, which will generate the HTML for the actual menu items, into the menu.jsp, between the opening and closing tags. I know I could break it up into two different jsp files, one for the start tag, and one for the end tag, but that seems sloppy.

Is it possible to do this?

Thanks!

+1  A: 

I really suggest you use jsp 2.0 tag files instead of the approach you suggest. Jsp 2.0 tag files were created to address problems like the one you describe. Combined with jstl (and possibly java classes if it's really complex), you should be able to achieve the same ends but with much cleaner means, with a much nicer separation of code and presentation.

krosenvold
A: 

If every page in your webapp needs the same menu, then you might consider Tiles, for defining Composite Views

toolkit