jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Boyd Waters <bwat...@cv3.cv.nrao.edu>
Subject Using PageContext to pass Object params
Date Sat, 23 Dec 2000 03:22:35 GMT
Mike Cannon-Brookes wrote:

> the current include methods do not allow you to pass parameters
> which are not strings.

CORRECT! But I don't see how your idea of JSPT gets away from passing
the things around as strings. Could you help me understand that?

I think you can pass Objects as "parameters" to tags by stuffing the
Object into a named slot in the pageContext, and then telling the tag
where to get it. In your page you say

<% pageContext.setAttribute
  ( "New Zealand English",
    new java.util.Locale( "en", "NZ" );
%>
.
. (more HTML page here, perhaps...)
.
<dt:timestamp locale="New Zealand English" usePageContext="locale" />

Instead of passing a characterization of an object (which must be parsed
and then used to call a constructor or whatever), I think I like the
idea of passing a String that tells the tag how to look the Object up.

If you're worried about namespace collision, you could adorn your
pageContext attribute names with the name of the tag or some other
convention:

<% pageContext.setAttribute
   ( "datetime.calendar:locale",
     new java.util.Locale( "es", "ES" );
%>

...and then by default my calendar tag finds any attributes by searching
the pageContext attribute space for any that happen to be defined. But I
don't think namespace pollution is much of an issue here.

So to summarize:

--
1) Reasonable default behavior (no params set):

<dt:timestamp />

--
2) Use a String parameter to figure out how to do something:

<dt:timestamp locale="fr CA" />

The String param is passed to the tag and it has to parse it to figure
out how to do the right thing. In this example, the locale param String
is tokenized into two Strings which are in turn passed to the
constructor new java.util.Locale("fr", "CA") to get the French-Canadian
locale.

--
3) Use the pageContext to set "global" attribs, which may be Object
values.

<dt:timestamp
  value="Last Edited Date"
  locale="French-Canadian"
  format="MMMM dd, yyyy
  usePageContext="value,locale" />

The tag in this example sees the "usePageContext" attrib, and therefore
knows to try to look up the "value" and "locale" values:

 dateValue = (Date)   pageContext.findAttrib( "Last Edited Date" );
 lc        = (Locale) pageContext.findAttrib( "French-Canadian"  );

Note that the tag in this example sets its "format" attrib based on the
String that's passed as the parameter. So you can mix and match.

--
4) Use some sort of namespace convention to magically glean attribute
values from the pageContext.

<% pageContext.setAttribute
  ( "datetime.timestamp:format",
    "dd/MM/yyyy HH:mm" );
%>

<dt:timestamp
  locale="French-Canadian" usePageContext="locale" />

In this example, the value of the timestamp will be the default, since
it is not set anywhere else. The other values are set like this:

 lc     = (Locale) pageContext.findAttrib( "French-Canadian"  );
 format = (String) pageContext.findAttrib( "datetime.timestamp:format"
);

Note that the locale is looked up exactly as in the previous example.


I don't like this last idea very much. I think that example 3) is
sufficient.



-- boyd

==============




PS:
I worked through Mike's example of an HTML box and couldn't find a place
where strings were not sufficient, where you had to pass a Java Object.
Given that HTML is strings, you characterize the colors as strings
anyhow...

Um, in the worst case, I'd draw a box using a CSS-styled DIV tag for
recent browsers, and a table for everyone else:

<% if( useragent.version > 4.0 ) { %>
<div style="color: #ffffff; background: #333333; border: 2px solid
black;">
<% } else { %>
<table border="2" background="#333333"><tr><td>
<% } %>

my content

<% if( useragent.version > 4.0 ) { %>
</div>
<% } else { %>
</tr></td></table>
<% } %>

(I'm hand-waving about the details of determining which content form.
That would be part of the implementation, maybe a hash table of
useragents and their capabilities. An old idea, not what I want to focus
on at the moment...)

Say that you want to use this idiom over and over again. So you want a
"box" tag that's part of an HTML taglib:

<html:box foreground="#ffffff" background="#333333" border="2">
my content
</html:box>

One could suppose, you'd pass a CSS-style structure (object?) that would
be used to produce the proper HTML. Hmm... that would be interesting,
because the semantics of Style would be passed along in the Style
Object, and could be used to emit HTML of various sorts...


-- 

---------
Boyd Waters                                          bwaters@nrao.edu
National Radio Astronomy Observatory              http://www.nrao.edu
PO Box 0 Socorro, NM 87801                               505.835.7346

                                        http://www.zocalo.net/~waters
                                                    waters@zocalo.net
---------

Mime
View raw message