Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put my taglib declaration?

Tags:

jsp

jstl

I've got a JSP and I'm going to start using the JSTL taglib. So I need to declare it and I do it by the row

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

But where do I put this code? At the top of the file, before everything, or after imports? Does it matter?

like image 334
Niklas Rosencrantz Avatar asked Apr 17 '12 09:04

Niklas Rosencrantz


People also ask

Can we use taglib in HTML?

This taglib contains tags used to create struts input forms, as well as other tags generally useful in the creation of HTML-based user interfaces.

What is use of taglib directive?

The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides means for identifying the custom tags in your JSP page.

Is taglib a directive?

The taglib Directive Specify a shortcut URI, as defined in a web. xml file (see "Use of web. xml for Tag Libraries" above). Fully specify the tag library description (TLD) file name and location.


3 Answers

Usually right at the top of the file. If you start using multiple taglibs you could also move it to a separate include file to safe typing. For example

/WEB-INF/jspf/taglibs.jspf

<%@ page contentType="text/html;charset=utf8"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

/WEB-INF/jsp/index.jsp

<%@ include file="/WEB-INF/jspf/taglibs.jspf" %>
like image 134
Jörn Horstmann Avatar answered Sep 28 '22 03:09

Jörn Horstmann


You put the taglib declaration right at the top of the file before everything else.

like image 24
Deco Avatar answered Sep 28 '22 01:09

Deco


I usually put it before anything, even before the <%@ page %>.

Just a little trick to have a cleaner HTML code generated, put them like this:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"
%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
%><html>
...
</html>
like image 37
sp00m Avatar answered Sep 28 '22 01:09

sp00m