Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL taglibs not recognized when declared in common header

Tags:

include

jstl

I had the idea a while back to put all of my taglib declarations (uri's, etc) in a common header file so I don't have to manually write them into all of my JSPs. Initially, things seemed fine, although I don't use the actual taglibs as much as just the simple EL syntax. However, I'm having trouble in all jsp files except for the one that explicitly has the taglibs declared. All of the other jsp's (that include the header file) treat the <c:something.../> tag as if it's HTML and don't evaluate it. I did some googling and found this post on O'Reilly suggesting that what I'm trying to do can be done, but I'm clearly doing something wrong. What's more is that the other stuff in the header file (common page headers, page titles, etc.) all show up fine. The header file and a sample of the inclusion are below.

Header file:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page session="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

Inclusion statement:

<jsp:include page="/WEB-INF/jsp/include/header.jsp">
    <jsp:param name="title" value="Home" />
</jsp:include>
like image 336
Chris Thompson Avatar asked Nov 30 '10 21:11

Chris Thompson


1 Answers

This is expected behaviour.

When you use <jsp:include>, it executed the target in a separate request, and then includes the output in the including JSP. It doesn't include the source of the included target, it includes the output. The means by which that target output is generated is lost.

To do what you're trying to do, you need to use <% include %> directives:

<%@ include file="/WEB-INF/jsp/include/header.jsp" %>

This will incline the literal text of header.jsp into your page. Of course, by doing that, you can no longer pass parameters to it, so you'd need to set that as a page context attribute (e.g. using <c:set>... but of course you can't use <c:set> until you've done your include...).

Essentially, it's not really worth the hassle. Taglib declarations are annoying boilerplate, but hard to get rid of.

like image 104
skaffman Avatar answered Sep 28 '22 06:09

skaffman