You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mike Campbell <mi...@s1.com> on 2001/02/08 00:09:52 UTC

bean tag parameter confusion

I have an action class in which I've instantiated a bean, and called
request.setAttribute("output", <my bean here>);
 
Now, in my JSP, I'm trying to "get at" that bean with <bean:define...>.  I'm
having a bit of trouble understanding the difference between the "id" and
the "name" parameters.
 
I can't use <bean:define name="output" type="the.correct.type">, since the
"id" parameter is required.  However, <bean:define id="output"
type="the.correct.type"> gives me an error. (null pointer exception in the
page context findAttribute() call).
 
What DOES seem to work is <bean:define id="anythingIChoose" name="output"
type="the.correct.type"> followed by <bean:write name="output"
property="someBeanProperty">
 
Also curiously (to me anyway), <bean:write name="anythingIChoose"
property="someBeanProperty"> seems to work if preceeded by the above
<bean:define...>.
 
Can someone tell me what the id and name parameters do for bean:define, when
all I'm looking to do is reference a bean on the request put there by the
action class?  It appears that id, although required by the TLD, needn't be
used, though it CAN be.  What's the preferred pattern here?
 
 
-- 
Mike Campbell	 Email:  <ma...@s1.com> mike.campbell@s1.com

S1 Corporation	 voice: 678.421.4641	
Software Engineer	 fax: 678.421.4865	
R & D	 web:  <http://www.s1.com/> http://www.s1.com/	
 <http://www.cmi-lmi.com/kingdom.html> Black Knight	
 

Re: bean tag parameter confusion

Posted by Mike Campbell <ug...@unixgeek.com>.
>>>>> "CM" == Craig R McClanahan <Cr...@eng.sun.com> writes:


CM> When you say you want to "get at" that bean, what is it exactly
CM> that you wish to do? For example, if all you want to do is render
CM> the value of the bean (or a property of the bean) to the output
CM> page, you should use <bean:write> instead of <bean:define>.

Ah, yes, I see.  I thought I had to "reference" the bean to make it
visible somehow to the <bean:write> tag before I could use that.  

CM> In <bean:define>, the "id" attribute tells you the name of the attribute
CM> (and scripting variable) that will be *created* by this tag -- exactly
CM> the same as the "id" attribute on the standard <jsp:useBean> tag works.
CM> The "name" attribute, on the other hand, is the bean *from* which you
CM> are copying the value to be created.


I see now why when I was using <bean:define>, my example could use
<bean:write name="_" property="foo" /> with "_" being the value of
either the "id" or "name" parameter of the <bean:define> call; I was
creating a copy of my action class inserted bean so the <bean:write>
was rendering the property of either the original or the copied bean.

-- 

Re: bean tag parameter confusion

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.

Mike Campbell wrote:

>  I have an action class in which I've instantiated a bean, and called
> request.setAttribute("output", <my bean here>);Now, in my JSP, I'm
> trying to "get at" that bean with <bean:define...>.  I'm having a bit
> of trouble understanding the difference between the "id" and the
> "name" parameters.I can't use <bean:define name="output"
> type="the.correct.type">, since the "id" parameter is required.
> However, <bean:define id="output" type="the.correct.type"> gives me an
> error. (null pointer exception in the page context findAttribute()
> call).What DOES seem to work is <bean:define id="anythingIChoose"
> name="output" type="the.correct.type"> followed by <bean:write
> name="output" property="someBeanProperty">Also curiously (to me
> anyway), <bean:write name="anythingIChoose"
> property="someBeanProperty"> seems to work if preceeded by the above
> <bean:define...>.Can someone tell me what the id and name parameters
> do for bean:define, when all I'm looking to do is reference a bean on
> the request put there by the action class?  It appears that id,
> although required by the TLD, needn't be used, though it CAN be.
> What's the preferred pattern here?--
>

  Mike Campbell     Email: mike.campbell@s1.com
  S1 Corporation    voice: 678.421.4641
  Software Engineer fax: 678.421.4865
  R & D             web: http://www.s1.com/
  Black Knight
>

When you say you want to "get at" that bean, what is it exactly that you
wish to do?  For example, if all you want to do is render the value of
the bean (or a property of the bean) to the output page, you should use
<bean:write> instead of <bean:define>.

In <bean:define>, the "id" attribute tells you the name of the attribute
(and scripting variable) that will be *created* by this tag -- exactly
the same as the "id" attribute on the standard <jsp:useBean> tag works.
The "name" attribute, on the other hand, is the bean *from* which you
are copying the value to be created.

Craig McClanahan