Can include a jsp in a custom TagSupport class, and specify where the body should go?

StackOverflow https://stackoverflow.com/questions/533405

  •  22-08-2019
  •  | 
  •  

Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

You can create custom tag like...

template.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>template</short-name>
  <uri>/template</uri>
  <tag-file>
    <name>block</name>
    <path>/META-INF/tags/template/block.tag</path>
  </tag-file>
  <tag-file>
    <name>stringBlock</name>
    <path>/META-INF/tags/template/stringBlock.tag</path>
  </tag-file>
</taglib>

Implementation

<%@tag import="org.mentawai.template.*" pageEncoding="UTF-8"%>
<%@attribute name="id" required="true"%>

<%
  Page page = (Page)request.getAttribute(TemplateServlet.PAGE_ATTR);
  Page block = page.getBlock(id);
  String view = "";
  if (block == null) {
      throw new ServletException("Block " + id + " doesn't exists");
  }
  request.setAttribute(TemplateServlet.PAGE_ATTR, block);
  view = block.getView();
  TemplateServlet.executeListener(block, request, response, application);

  String oldView = (String)request.getAttribute("_view");
  request.setAttribute("_view", "/" + view);
%>

<jsp:include page="${_view}" flush="true"/>

<%
  request.setAttribute("_view", oldView);
  request.setAttribute(TemplateServlet.PAGE_ATTR, page);
%>

Reference: http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top