Here's an off topic idea I've been pondering about lately...


I like JSP and custom tags, that we can astract away complex presentation or
business logic away behind XML tags. I like the fast development cycle of
JSP as well - edit a JSP, press reload in your browser and voila!

I've noticed that alternatives to JSP use XSLT to implement "tags". For
example Cocoon's XSP and Resin's XTP

http://xml.apache.org/cocoon/xsp.html
http://www.caucho.com/articles/xtp_templates.xtp

It got me wondering, how about adding XSLT based "compile time" tags to our
toolset when using standard JSP?

What's required? Well just that we use the XML format for JSPs and that we
apply an XSLT document to our JSPs before our servlet engine tries to run
them. This XSLT can then preprocess certain XML tags to do simple formatting
or adding dynamic code. The tags could be put into namespaces just like
regular JSP custom tags. More on what we can do with compile time tags
later...


----
Implementation:

If the servlet engine doesn't support the XML format fully (which I've found
to be true in many engines I try) then we could always use this XSLT
preprocessing stage to turn the XML format of JSP into the standard style
<%= %> format.

Right now today we can implement XSLT preprocessing using an Ant build
system to build all JSP files before use our web application.

However this breaks our nice fast "edit JSP & reload browser" development
environment. So it would be cool if we could integrate the XSLT
'preprocessing' stage directly into our JSP compiler.

For example it would be great if we had a configuration hook to, say,
Tomcat/Jasper that for a certain web-app we wish to apply a certain XSLT
stylesheet to preprocess all of the XML format JSP files. If it were
implemented in Jasper, we could then reuse Jasper across other servlet
engines (Jetty, WebLogic et al) to get the same feature in other app
servers. In production, we may choose to use the Ant build process approach
to do the XSLT preprocessing as the JSP probably won't be changing much.

I wonder if anyone on the list has tried this idea?

I've taken a quick peek at the Jasper source code and it looks like it
shouldn't be too hard to add some kind of configurable pre-processor into
it. (Though it would be great if the JSP spec had an entry for XSLT
pre-processing of JSP using the web deployment descriptor ;-).


----
Examples:


Compile time tags can only use compile time attributes, though they could
output JSP scriptlet expressions.  For example here's an example of a normal
regular HTML form in a piece of JSP.


 <FORM action="something.jsp" method="get">
    <P>
    First name:
    <INPUT  type="text" name="firstname" />
    <BR />
    Last name:
    <INPUT  type="text" name="lastname"  />
    <BR />
    email:
    <INPUT  type="text" name="email" />
    </P>
    <BR />
    <INPUT type="submit" value="Send">
    <INPUT type="reset">
 </FORM>

Using XSLT preprocessing, we could preprocess the above using XSLT to
include scriptlet expressions to fill in the values of the fields based on
the current state of the form:-

 <FORM action="something.jsp" method="get">
    <P>
    First name:
    <INPUT
        type="text" name="firstname"
        value='<%= getFormValue( pageContext, "firstname") %>'/>
    <BR />
    Last name:
    <INPUT
        type="text" name="lastname"
        value='<%= getFormValue( pageContext, "lastname") %>'/>
    <BR />
    email:
    <INPUT
        type="text" name="email"
        value='<%= getFormValue( pageContext, "email") %>'/>
    </P>
    <BR />
    <INPUT type="submit" value="Send">
    <INPUT type="reset">
 </FORM>

Where the function "getFormValue()" can be used to fetch the values of the
fields in the form using any real Java code, maybe a custom form package or
by just pulling the values out of the servlet request parameters.

This means that from the developer / designers perspective the JSP remains
'clean' and HTML like without the scriptlet expressions it - all the clever
stuff happens when we compile this JSP into Java code.

Also note that this means we don't need to write a Jsp custom tag for each
type of <input> tag, having JavaBean properties for all possible attribute
values we wish to use. At run time no tags are created, the only bit of
dynamic Java code on the page will be the calls to getFormValue(). So the
page will be really effiicent.


Note that I'm not advocating a replacement of JSP custom tags here - just
advocating the use of both XSLT preprocessing with JSP custom tags as a
really neat way of building web-apps. Doing XSLT preprocessing in an 'ant
build process' is a bit of a bind for a JSP developer so I'm advocating its
inclusion into the 'JSP compile' step.


Comments?

<James/>


James Strachan
=============
email: [EMAIL PROTECTED]
web: http://www.metastuff.com

__________________________________________________________________

If you are not the addressee of this confidential e-mail and any 
attachments, please delete it and inform the sender; unauthorised 
redistribution or publication is prohibited. 
Views expressed are those of the author and do not necessarily 
represent those of Citria Limited.
__________________________________________________________________

Reply via email to