Steve Steve - 1 year ago 48
Java Question

How to avoid redeclaring tag libraries in included JSP files?

I made myself a template for all JSPs in my webapp that keeps things very simple:

<%@ include file="header.jsp" %>

<!-- Put unique content here -->

<%@ include file="footer.jsp" %>

I decided to move my navigation menu out of header.jsp into its own file as it is getting larger. I put the code for the menu into menu.jsp. Here is how the include looks inside of my header.jsp:

<title>My JSP</title>


<jsp:include page = "menu.jsp"/>


However, menu.jsp will not work unless I also put this line in it:

<%@ taglib prefix = "c" uri = ""%>

That is already in my header.jsp. I would like to only to have to put it there. However when I try using this sytax instead:

<%@ include file="menu.jsp" %>

I get an error message from WebLogic 11g stating that

<%@ include file="header.jsp" %> is self refrencing

How can I include menu.jsp in header.jsp without having to include the JSTL in menu.jsp?

Answer Source

A JSP page included dynamically (<jsp:include />) must have a taglib declaration if it uses the taglib because it is compiled separately.

A JSP page included statically doesn't have the same restriction because it's compiled into the servlet itself.

What's the big deal about declaring a taglib in a JSP in which it's used? That makes it a standalone "chunk" of functionality, dynamically includable anywhere, self-documenting, without worrying about the page it's being included in: this is the goal of breaking up functionality.