jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hans Bergsten <h...@gefionsoftware.com>
Subject Re: best practices discussion: tag attribute versus pageContext
Date Fri, 15 Dec 2000 21:52:24 GMT
Daniel Kehoe wrote:
> 
> I'm working on a reporting application that uses a model-view-controller
> architecture. I'd like some "best practices" advice.
> 
> My Model sets text in a Results bean that becomes a Request attribute:
> resultsBean.setText("some string");
> request.setAttribute("results", resultsBean)
> 
> First question (more to follow):
> 
> In a tag handler class I can obtain the text from the Results bean through
> the pageContext property:
> pageContext.getRequest().getAttribute("results").getText()
> in which case the page can use the simple tag:
> <mytags:displayText />
> 
> or I can expose the Results bean in the page and pass the text as an
> attribute of the tag:
> <jsp:useBean id="results" class="Results" scope="request" />
> <mytags:displayText text="<%= results.getText() %>" />
> 
> The first option makes the tag handler dependent on knowing that there is a
> results attribute in the request with a getText() method.
> 
> The second option adds clutter to the JSP.
> 
> What's best practice here? I'm tending to the latter because I get a more
> portable tag handler.

Why not add a name/property attribute pair to your custom action,
so you can do this:

  <jsp:useBean id="results" class="Results" scope="request" />
  <mytags:displayText name="results" property="text" />

You can use the PageContext.getAttribute() method in your tag handler,
and get the result bean from any scope, and use bean introspection to
allow the text to be retrieved from any property. This also makes
it consistent with the conventions established by <jsp:getProperty>
and <jsp:setProperty>.

Hans
-- 
Hans Bergsten		hans@gefionsoftware.com
Gefion Software		http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com

Mime
View raw message