tags:

views:

119

answers:

2

I am studying JSTL part of JSP, I have a question about the <c:forEach> loop tag. Is that c prefix required? Does it indicate any specification?

+6  A: 

You need some prefix, but it doesn't have to be c:, that's just the convention. The prefix is specified when you declare the tag library at the top of the JSP, and it can be anything you like (within reason). The important part is the namespace URI (also specified in the JSP header), and that has to be a specific value (http://java.sun.com/jsp/jstl/core in this case).

But you must have a prefix of some form, otherwise the JSP engine won't recognise it.

skaffman
+8  A: 

Tag libraries in JSP are identified by tag library URIs. When you use a tag library in your JSP page, you import a tag library by its URI and bind it to some prefix:

<%@taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

After that you use the specified prefix to indentify tags from that library:

<c:foreach ...>

EDIT: These prefixes can be arbitrary, but there is a convention to use a value of the <short-name> field in the tag library definition file (it's where c for JSTL Core comes from)

axtavt
Keeping convention is important, as it makes the tags readable without having to check the top bindings all the time.
Thorbjørn Ravn Andersen