This chapter will show you how to use your own Tag Libraries (taglibs) with iQgen. We will use a Tag Library to format date and time data as an example. iQGen supports JSP 1.1 Tag Libraries. If you want to know how to program tag libraries, please consult the JSP 1.1 specification or any JSP tutorial.
Tag Libraries enable you to write simpler templates. Basically they allow you to move complicated logic from your templates into custom tags. Templates often are much easier to read, understand, and maintain, when Tag Libraries are used. They can be structured more clearly. As a consequence, you should use Tag Libraries whenever you feel that your templates are getting too complicated and unreadable.
Basically you can deploy every JSP 1.1 Tag Library into iQgen. The library has to meet certain criteria, though: First of all, the Tag Library has to be packed into a jar file and this jar file must contain a Tag Library Descriptor (TLD). The TLD is a file that describes the tags contained in the Tag Library. It is best - but not required - to be placed in the META-INF directory for compatibility with some IDEs e.g. IntelliJ IDEA™. The JSP engine uses this file to get all the necessary information to process the Tag Library.
Now the Tag Library can be deployed. You have to copy it into a subdirectory taglibs in the home directory of your iQgen installation (e.g. C:\Programme\iQgen). If the directory does not exist, you have to create it. Now this Tag Library can be used in all iQgen templates.
If the deployed Tag Library uses any libraries (jar files), they have to be put into a subdirectory of the taglibs directory called lib.
In case you want to use the Ant interface of iQgen a special issue arises. The jar file of the Tag Library then has to be placed in a taglibs directory in the lib directory of your Ant installation. The reason for this is that iQgen searches the jars of Tag Libraries relatively to the iQgen jar file. This jar file - in the Ant mode - resides in the lib directory of Ant.
If you have completed all the steps explained in the previous paragraph, you can use your Tag Libraries in your templates. Here is some example code which shows you how to access a Tag Library:
[...] <%@taglib uri="your_taglib" prefix="your_prefix" %> [...] <your_prefix:your_tag your_attr1="a" your_attr2="b"> In case of a body tag there can be something in here </your_prefix:your_tag> [...]The taglib directive ( <%@taglib ... %>) makes a Tag Library available within a template. To do this the URI of the Tag Library and the prefix must be specified as attributes of the directive. The URI of a Tag Library in the taglibs directory is the filename of the jar file of the Tag Library without .jar It is used to locate the Tag Library. Now you can use the Tag Library identifying it using the prefix, you have specified (i.e. <your_prefix:your_tag ... >).
The iQgen Sample Tag Library is included in the iQgen distribution, you can find it in the directory samples/getting_started/taglib_tutorial in your iQgen directory.
The Java class com.innoq.generator.samples.taglibs.SampleTag implements the date and time formating feature. The following TLD is used:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>iqgen-taglib-sample</shortname>
<info>Sample which shows you how to use the taglibs</info>
<tag>
<name>date</name>
<tagclass>com.innoq.generator.samples.taglibs.SampleTag</tagclass>
<info>Formats Dates</info>
<attribute>
<name>time</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>date</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>format</name>
<required>false</required>
</attribute>
</tag>
</taglib>The Tag Library contains only one tag, which
is called date. The attributes are
time, date and
format. The allowed values for the attribute
format are date,
time and datetime,
which is the default. If none of these values is given, the default
will be used.The tutorial includes an Ant build file ( build.xml), which can be used to compile, assemble and install the Tag Library. Just have to call the target install, respectively install-for-ant in order to make this Tag Library available for the GUI or the Ant interface of iQgen.
The important parts of the build file to illustrate what is done to deploy the Tag Library are:
[...]
<target name="dist" depends="compile">
<copy file="${basedir}/taglib.tld" todir="${classes.dir}/META-INF"/>
<jar jarfile="${jar.file}" basedir="${classes.dir}"/>
</target>
<target name="install" depends="dist">
<mkdir dir="${generator.dir}/taglibs"/>
<copy file="${jar.file}" todir="${generator.dir}/taglibs"/>
</target>
<target name="install-for-ant" depends="dist">
<mkdir dir="${ant.home}/lib/taglibs"/>
<copy file="${jar.file}" todir="${ant.home}/lib/taglibs"/>
</target>
[...]As you can see the taglib.tld is
copied into the META-INF directory of the jar file
in order to build the jar. This ensures the correct structure of the
jar file. The install and
install-for-ant targets then copy the jar
file into the correct directory.After having successfully installed the Tag Library there will be a taglibsample.jar in the taglibs directory of your iQgen installation. Now you can use the taglibsample Tag Library in your templates:
[...] <%@taglib uri="taglibsample" prefix="datelib" %> [...] <datelib:date format="date"/> <datelib:date time="<%=System.currentTimeMillis() + 3600000%>" format="datetime"/> [...]
This will result in the following output:
09.08.2002 09.08.2002 11:06:43