![]() | ![]() | ||||||||
|
The XTP templates example is an expansion of the JSP "model 2" example. You may want to refer to Resin's support of XSL and JSP. The guest book uses three application-specific tags
Compare the login.xtp page with the
login.jsp in the JSP
templates example.
HTML tags should be copied to the generated output. So we just create a default rule to copy the input to the output. XSLT is a pattern based language. 'node()' matches any node and '@*' matches any attribute. So any tag that doesn't match any other rules will be copied. It's important to notice that XSLT works with XML trees, not text
streams like JSP. The xsl:copy of an attribute adds and attribute
to the containing element.
XSLT-lite makes scanning templates easier because the template
pattern is clear, on its own line. The following is exactly
equivalent to the defaultcopy.xsl above.
guestbook.xsl contains the custom tags for the guestbook example.
When Resin looks at a node, it will select the best matching pattern
and execute it. The element <ct:guest-book> will generate a loop,
while <form action='GuestXtp'> will copy the form to the output.
So now we've created some tags for a custom application. That's nice and it shows how XSL can clean JSP pages. The guest book JSP was fairly simple so the added complexity of creating stylesheets doesn't really show how powerful the XTP concept is. For a taste of real power, we'll create a simple form library. request.setAttribute to set default values for
form element. For example, once a user has added a comment, she can
edit the comment. The default value is the old comment.
In fact, the w3c now has a new requirements document for the next generation of intelligent forms. That doesn't really help web designers because web sites still need to support old browsers. XTP gives web sites the more powerful functionality now. There are a few other common form extensions:
The XTP file itself is simple. It just looks like any HTML page,
except form values are called ct:*.
The transformation is relatively simple. Copy all properties to
the output and add a value attribute if the servlet supplied a default value.
XSLT lets stylesheets pull common formatting code into functions using the named template mechanism. The template functions are called with xsl:call-template. xsl:call-template works like xsl:apply-templates, but it passes the current node, not the children nodes. Because the generated JSP will add an attribute at run time, the form.xsl treats its output as unparsed content. Unknown elements, like <form> or <input> are printed as text. Unparsed content works just like JSP. fun:copy-attrs is a simple XSLT function which copies the attributes from the XTP element to the output. In the ct:input example above, it will copy name=Name and size=40. xtp:cache tells Resin that the XSL results are cacheable. Once
it's generated the JSP, it doesn't need to run the XSL engine again.
This greatly improves the performance.
fun:input-attrs is the core of the form processing. It calls
fun:copy-attrs to copy the user's attributes. Then it generates the
JSP to add a value attribute if it's available.
Now that the functions have been defined, creating the individual
tags is simple.
An important value of XTP is that it lets web pages use the latest W3C specifications without waiting for all the browsers to support it. For example, the W3C has proposed requirements for intelligent forms. A properly designed form.xsl can implement intelligent forms on the server and support all browsers, not just the browsers supporting the spec.
Copyright (c) 1998-2008 Caucho Technology, Inc. All rights reserved. resin® and quercus® are registered trademarks, and Ambertm is a trademark of Caucho Technology, Inc. |