How do I precompile my JSTL jsps?
Author: Deron Eriksson
Description: This tutorial describes how to precompile your JSTL JSPs using the maven jspc plugin.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1) || Tomcat 6.0.14


Page:    1 2 >

In another tutorial, we saw how we could precompile a jspW using the mavenSW jspc plugin. If a project utilizes JSTLSW on its jspsW, the project needs to reference the jstlSW and standard dependencies in order for the jsp precompiler to work. In fact, these dependencies also need to be present in order to debug the project in EclipseSW. Here is an example of a jsp that uses JSTL:

index.jsp

<%@ page contentType="text/html; charset=ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>

<html>
<body>
<h2>Hello World!</h2>
<c:out value="Hello again" />
</body>
</html>

If my project is set up for jsp precompilation and I try a "mvn package" on the project without the required dependencies, an error will occur since the JSTL jars are not available.

Console output from 'mvn package' without JSTL dependencies

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building mywebapp Maven Webapp
[INFO]    task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [jspc:compile {execution: jspc}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] JSPC Error

Embedded error: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Fri Feb 08 11:23:48 PST 2008
[INFO] Final Memory: 6M/11M
[INFO] ------------------------------------------------------------------------

To correct this, I'll add the jstl and standard jars as dependencies to my pom.xml. I'll set the scope to be provided since my servletW container (Tomcat) will provide these libraries. If you want the jars to be packaged in the project warW file, you can set the scope to be "compile". You could also remove the scope element, since by default it will be "compile".

JSTL dependencies

		<!-- begin - jstl dependency-->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.1.2</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
			<scope>provided</scope>
		</dependency>
		<!-- end - jstl dependency-->

If I perform a "mvn package" with the new JSTL dependencies added to my pom.xml, the war file builds correctly, and the JSTL-based index.jsp file gets precompiled.

Console output from 'mvn package' with JSTL dependencies

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building mywebapp Maven Webapp
[INFO]    task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to C:\dev\workspace\mywebapp\target\classes
[INFO] [jspc:compile {execution: jspc}]
[INFO] Built File: \index.jsp
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] No sources to compile
[INFO] [surefire:test]
[INFO] No tests to run.
[INFO] [war:war]
[INFO] Packaging webapp
[INFO] Assembling webapp[mywebapp] in [C:\dev\workspace\mywebapp\target\mywebapp]
[INFO] Processing war project
[INFO] Webapp assembled in[82 msecs]
[INFO] Building war: C:\dev\workspace\mywebapp\target\mywebapp.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Fri Feb 08 12:53:59 PST 2008
[INFO] Final Memory: 10M/19M
[INFO] ------------------------------------------------------------------------

(Continued on page 2)

Page:    1 2 >