In JSP is it necessary to redeclare tag libraries in included files?
Question
Im not really familiar with how this works in JSP
but in the
main.jsp template there is this:
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<jsp:include page="/jsp/common_head.jsp"/>
then in common_head.jsp we have this again:
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
is this necessary?
or in other words
Are taglib declarations propagated to include files?
Solution
Yes, this is necessary. Before executed for the first time, every JSP file will individually be converted/translated/compiled to a standalone Servlet
class. All tags will be translated to "real" Java code/methods. If you don't declare a taglib, then the JSP compiler don't know what Java code/methods it need to generate/call for the particular tags.
In case of for example Tomcat, take a look in the /work
folder for all of those compiled JSP's. View their source and it'll be all clear.
OTHER TIPS
The
<jsp:include page="/jsp/common_head.jsp"/>
... tag is a dynamic include meaning that it dynamically calls the common_head.jsp page, which is compiled independently of the including page. Thus the taglib directive should be needed.
If, on the other hand you were to do a static include using the include directive
<%@ include file="/jsp/common_head.jsp" %>
... the file would have been copy-pasted and and compiled with the page from which it is included. Then the taglib directive should not be needed.
In any case you may want to have the taglib included just to get editor support of the tags you use during development.
Note that static files are statically included, even with the jsp:include tag
include directive: http://java.sun.com/products/jsp/syntax/1.2/syntaxref129.html#997991
jsp include: http://java.sun.com/products/jsp/syntax/1.2/syntaxref1214.html
As you're including a dynamic resource, that resource is "standalone" so it should include any tag library that you are using. Other question is if you're using those tags...
Yes it is required for JSP compilation . You can also use below taglib instead of c.tld.
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
hope this will help
In other template systems the definitions usually carried over. I dont see why you should have to use it again