views:

87

answers:

4

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?

+1  A: 

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...

rossoft
ok, but the included files are never used "standalone", they are always included. you never access common_head.jsp directly since it generates the <head></head> content of the main html file
codeninja
yes, but your usage does not affect how should be declared it. The JSP file is compiled whenever you access it, so when common_head.jsp is compiled, it should have declared all the tags it needs.
rossoft
see this comment:http://mail-archives.apache.org/mod_mbox/tomcat-users/200112.mbox/%[email protected]%3Eif you use instead <%@include ... />, then you would not need to redeclare them, as it is "inserted" in compile time
rossoft
+1  A: 

In other template systems the definitions usually carried over. I dont see why you should have to use it again

yoodle
thanks for your feedback
codeninja
+1  A: 

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.

BalusC
thanks for your feedback. Perfect answer.
codeninja
+1  A: 

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

Christoffer Soop
thanks this information was very useful
codeninja