Harris Wulfson wrote:
>
> I am using Tomcat.
>
> Question: Could the <jsp:useBean> tag be implemented as a custom action?
>
> More generally: Can a custom action make an object available to the JSP
> script,
> without requiring the page to grab the variable out of the pageContext?
>
> Here are examples of what I have, vs. what I would like to see. Any
> thoughts?
>
> 1. what I have:
>     <me:loop vector="<%=stuff%>" item="aThing">
>         <jsp:useBean id="aThing" class="package.Thing" scope="page">
>         <%// OR:  Thing aThing = pageContext.getAttribute("aThing"); %>
>         <%=aThing.getRelatedWidget().getName()%><br>
>     </me:loop>
>
> 2. what I would like to see:
>     <me:loop vector=stuff item="aThing">
>         <%=aThing.getRelatedWidget().getName()%><br>
>     </me:loop>
>
> It is actually 2 seperate issues:
> a. can you pass variables into the action without using ugly <% %> tags?

If the variable is available in one of the scopes (page, request, session or
application, yes. The tag handler just need the name and can then get it
from the scope. For instance, something like this would work:

  <jsp:useBean id="stuff" class="package.Stuff" >
    <jsp:setProperty name="stuff" property="*" />
  </jsp:useBean>

  <me:loop name="stuff" property="listOfThings" item="aThing" >
    ...
  </me:loop>

listOfThings could be a property of Stuff that returns a Vector, an array, etc.

If "stuff" is not a bean with properties, but a Vector for instance (as seems
to be your case), you can let a servlet that processes the request (a "front
component") or a scriptlet put it in the right scope and use something like:

  <% // This stuff is better done in a front component servlet
     java.util.Vector stuff = new java.util.Vector();
     // Add your stuff in the Vector
     pageContext.setAttribute("stuff", stuff);
  %>

  <me:loop name="stuff" item="aThing" >
    ...
  </me:loop>

If "stuff" is not in a JSP scope at all, only available as a scripting variable,
you must use the <%= stuff %> syntax.

> b. can you get around having the script suck the resulting item out of
> the pageContext?

Sure, a custom action can declare that it creates a scripting variable that's
available within the body, or even after the body. Look at TagExtraInfo. You
need to define a TagExtraInfo subclass for your tag handler, implemented
something like this:

public class LoopTagExtraInfo extends TagExtraInfo {
    public VariableInfo[] getVariableInfo(TagData data) {
        return new VariableInfo[]
        {
            new VariableInfo(data.getAttributeString("item"),
                data.getAttributeString("class"),
                true,
                VariableInfo.NESTED)
        };
    }
}

You can then use the tag like this:

  <me:loop name="stuff" item="aThing" class="package.Thing" >
    <%= aThing.getRelatedWidget().getName() %><br>
  </me:loop>

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to