Tag Libraries
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Servlets and Filters
Portlets
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Introduction
User's Guide
Reference
Tutorials

StyleScript
Tag Libraries
Templates
StyleScript
Reference
Templates

XML Template Pages encourages web sites to create JSP tag libraries. By putting the JSP creation in stylesheets, you can separate the programming from the web pages. The XTP pages can be simple XML or HTML documents and simply add the custom tags as necessary.

The examples in this section use the full, verbose, XSLT and JSP syntax.

  1. Creating JSP from XTP
  2. Creating JSP flow control

Creating JSP from XTP

creates a JSP file from an XTP file. XTP pages can separate the content (in XML) from the scripting (in XSLT). The XSLT stylesheets now become a palette of JSP actions you can use just by adding tags.

  1. Parse XTP file as HTML
  2. Find and parse XSLT stylesheet
  3. Applying the stylesheet to the XTP, creating a JSP file
  4. Execute the JSP file

A trivial example is a named counter. If the counter has an 'id' attribute, we'll use it at the value of the application variable.

XTP pages can use the counter just by adding the tag:

counter.xtp
A counter example: <ct:counter id='test'/>

Here the patterns to do it. For efficiency, we've added the cache directive. The cache directive tells XTP to execute the stylesheet only once and cache the generated JSP file.

default.xsl
<xtp:directive.cache/>

<xsl:template match='counter[@id]'>
  <jsp:expression>
     <xsl:text>application.attribute["</xsl:text>
     <xsl:value-of select='@id'/>
     <xsl:text>"]<xsl:text>
  </jsp:expression>
</xsl:template>

<xsl:template match='counter'>
  <jsp:expression>
     application.attribute.counter++
  </jsp:expression>
</xsl:template>

The following JSP file is the result. will execute the generated JSP file to process HTTP requests. Because default.xsl was marked as cached, on following requests will merely reexecute 'gen438.jsp'.

gen438.jsp
A counter example: <jsp:expression>
  application.attribute["test"]++ 
<jsp:expression>

Creating JSP flow control
XTP can also generate JSP control. Many programmers like using tags for programming, like Cold Fusion. Here's how to create a basic set of programming tags.

  • ct:get prints the value of a variable
  • ct:if generates an if statement
  • ct:iter loops
An example use might print the HTTP headers.

<ct:iter expr='request.header'/>
  <ct:get expr='i'/> : <ct:get expr='request.header[i]'/>
</ct:iter>

Accept : image/gif, image/png
User-Agent : Mozilla/4.5
Accept-Language : en
Host : www.caucho.com

Here is the stylesheet:

definition of ct:get
<xsl:template match='ct:get'>
    <jsp:expression><xsl:value-of select='@expr'/></jsp:expression>
</xsl:template>

definition of ct:if
<xsl:template match='ct:if'>
    <jsp:scriptlet>
    if (<xsl:value-of select='@expr'/>) {
  </jsp:scriptlet>
    <xsl:apply-templates/>
    <jsp:scriptlet>}</jsp:scriptlet>
</xsl:template>

definition of ct:iter
<xsl:template match='ct:iter'>
    <jsp:scriptlet>
    for (var i in 
  <xsl:value-of select='@expr'/>) {
  </jsp:scriptlet>
    <xsl:apply-templates/>
    <jsp:scriptlet>}</jsp:scriptlet>
</xsl:template>


StyleScript
Reference
Templates
Copyright © 1998-2005 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.