struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject [PROPOSAL] Integrating Struts with XML Data
Date Mon, 09 Oct 2000 06:27:23 GMT
One of the desireable goals for Struts 1.0 is a closer integration with
XML-formatted data -- that is, a graceful way to deal with data sources
that produce XML data and wish to have their information displayed in an
HTML page.  One obvious candidate technology is to use for this purpose
is XSLT transformations to convert the XML data to HTML or WML or
whatever -- but writing XSLT stylesheets is not something that most web
developers will be very comfortable with.  An easier solution is needed
to deal with simple cases.

Given the fact that Struts now includes some very powerful bean-related
tags like <struts-bean:define>, wouldn't it be nice if we could extend
those tags to deal with XML data as well?  It turns out that this is
quite feasible ... the proposal below is an approach to extending the
Struts bean-related tags so that, when they are presented with a bean
that is an XML object (that is, an object of type org.w3c.dom.Document
or some reasonable subordinate object like Element), the bean tags
should extract property values from them similarly to the way they treat
Java objects.  The big advantage is, a page developer would be able to
use essentially the same tags for cases where the data was presented as
XML as when the data was presented as JavaBeans or EJBs.  (A similar
extension could be made later to deal with Rows and Columns in a JDBC
result set).

In the examples below, assume that a DOM object with the following
structure is attached to the bean name "bean":

    <a b="1" c="2">
        <d e="3" f="4"/>
        <d e="5" f="6"/>
    </a>
    <a b="7" c="8">
        <d e="9" f="10"/>
        <d e="11" f="12"/>
    </a>

Here's the proposal -- please let me know what you think:

(1) Extensions to <struts-bean:define>:

If the bean referenced by the "name" attribute is a DOM object, apply
the syntax for simple, nested, and/or indexed property syntax to extract
the appropriate element or attribute from the DOM according to the
following rules:

* If the final name in a nested property expression such as a.b.c
  identifies an attribute of a selected XML element, return it as a
String.
  Otherwise, return a DOM element representing the selected
  XML element.

* If a component of a property name expression is indexed,
  it is zero-relative in the usual way.  If it is not indexed, the
  implied index is zero (i.e. a reference to the "a" element in
  the example will return the first <a> element).

These rules cause the following behavior given a "<struts-bean:define
name="bean" property='xxx"/>" tag, given the following values for "xxx":

Simple property names:

    a --> (the first <a> element as a DOM object)

    b --> 1

    c --> 2

Nested property names

    a.b --> 1

    a.c --> 2

    a.d --> <d e="3" f="4"/>

    a.d.e --> 3

    a.d.f --> 4

Indexed property names:

    a[0] --> (the first <a> element as a DOM object)

    a[1] --> (the second <a> element as a DOM object)

    a.d --> <d e="3" f="4"/>

    a[0].d --> <d e="3" f="4"/>

    a[0].d[0] --> <d e="3" f="4"/>

    a.d[1] = <d e="5" f="6"/>

Combination property names (examples only, not exhausitive):

    a.d[1].e --> 3

    a[1].d[1].e --> 11

(2) Extensions to <struts-bean:write>:

If the object referenced by the "name" and optional "property"
attributes is a DOM object, pretty-print it in its XML format with
"&lt;" and "&gt" escapes for the angle brackets.  This is useful in
debugging, and for catching property naming expression errors.  It might
also be useful in a production application to do something similar to
the XSL "<xsl:select>" operation.

(3) Extensions to <struts-bean:iterate>:

If the object referenced by the "name" and optional "property"
attributes is a DOM object, iterate over the XML elements that are
nested inside the specified element, exposing each iterated element as a
DOM object:

    <struts-bean:iterate name="bean"> --> Iterate over both <a> elements

    <struts-bean:iterate name="bean" property="a"> --> Iterate over both
<d> elements of the first <a> element

    <struts-bean:iterate name="bean" property="a[0]"> --> Iterate over
both <d> elements of the first <a> element

    <struts-bean:iterate name="bean" property="a[1]"> --> Iterate over
both <d> elements of the second <a> element

    <struts-bean:iterate name="bean" property="a[0].d"> --> Iterate over
a zero length collection because there are no nested elements.

Note that, because the <struts-bean:write> tag can be used to extract
appropriate properties from the individual elements being iterated over,
you can use these tags to create reports based on a collection of
"objects" that are in fact represented as XML elements, in the same way
that you could create reports based on an ArrayList of Java objects that
have properties corresponding to the attribute names of the iterated
elements.

Craig McClanahan

====================
See you at ApacheCon Europe <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00):  Sun Technical Briefing
Session T06  (24-Oct 14h00-15h00):  Migrating Apache JServ
                                    Applications to Tomcat

Mime
View raw message