You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by SWC <ma...@crissman.net> on 2004/02/12 05:20:57 UTC

How to return a method binding value?

I have a component I am developing which I would like to have create a 
direct link during render.  In order to do this, I need a function that 
can return the appropriate listener at render.  What I cannot determine 
is how to make a method which will return a value appropriate for the 
listener binding of the DirectLink.

Hopefully an example clarifies what I am attempting:

in the component template...
for each link
  {
     <a jwcid="@DirectLink" listener="link.listener"  
parameters="ognl:link.parameters">
}

where the links will be changing, and thus possibly pointing to 
different methods at run time.  How do I create the getListener() 
method that would be needed in the above example to complete the 
binding correctly.

Hope I'm clear.  If not, I will certainly try to clarify.

Spencer


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: How to return a method binding value?

Posted by SWC <ma...@crissman.net>.
I ended up going with a form of this solution to fit my needs.  When  
I've finished the rest of the component, I'll make it available for  
some comments and/or suggestions, as I still feel like although I'm  
picking up the ins and outs of tapestry as I go, I've yet to learn  
component best practices.

Thanks again for all the help, both of you.

Spencer

On Feb 12, 2004, at 8:32 PM, Harish Krishnaswamy wrote:

> Without understanding your real situation its kinda hard to nail it.  
> Seems like the regular listener methods should work for you, something  
> like the following:
>
> <a jwcid="link" listener="ognl:listener" />
>
> .java
> public IActionListener getListener()
> {
>    // I hate these if .. else
>    // if it is as simple as this, you could create a map that
>    // maps your condition to the right listener
>    if (xCondition)
>        return getListeners().getListener("xListener");
>
>    if (yCondition)
>        return getListeners().getListener("yListener");
> }
>
> public void xListener(IRequestCycle cycle)
> {
>    ...
> }
>
> public void yListener(IRequestCycle cycle)
> {
>    ...
> }
>
> -Harish
>
> SWC wrote:
>
>> Alright, my understanding is expanding here, but I could still use  
>> just a touch of clarification.
>>
>> Howard wrote:
>> "The ognl:listener.foo mapping to method foo() makes use of Java  
>> reflection to create the connection,
>> but it is not required. Listeners objects don't have to be listener  
>> methods, you can use inner
>> classes, helper beans or what not ... as long as they implement the  
>> interface."
>>
>> So I can bind to any object that defines the IActionListener  
>> interface, but other than ognl, there is no way to pass in the method  
>> as a parameter directly? There's no way to create an IActionListener  
>> that wraps a method directly. Is that understanding correct?
>>
>> My options as I see them then, would be to do one of the following:
>> 1) Create tiny classes, one for each method I had wanted to bind to.  
>> Each would define a method which fulfilled the IActionListener  
>> interface, and the component could bind each of the links using one  
>> of those classes. I'm pretty limited in my knowledge of patterns, but  
>> I think maybe there is one which is appropriate here where I would  
>> construct a single object from which you could request the  
>> appropriate "method class" as needed?
>>
>> 2) Always bind to the same method, but pre-pad the parameters with a  
>> value that can be used to switch between a limited number of other  
>> functions from within that function.
>>
>> The first is more powerful, but seems like too much work for what I  
>> am trying to accomplish, and I don't know that the second will be  
>> flexible enough for sure.
>>
>> Either way, thanks to both of you for your input.
>>
>> Spencer
>>
>>
>> On Feb 11, 2004, at 11:34 PM, Harish Krishnaswamy wrote:
>>
>>> If you have a finite set of listeners that you want to juggle with,  
>>> you can create one listener that invokes the appropriate listener  
>>> and have all links bind to the one ubiquitous listener.
>>>
>>> -Harish
>>>
>>> Harish Krishnaswamy wrote:
>>>
>>>> AFAIK you can only achieve what you are trying to do with runtime  
>>>> code generation that is assuming you want different behavior for  
>>>> each link in the loop.
>>>>
>>>> -Harish
>>>>
>>>>
>>>> SWC wrote:
>>>>
>>>>> I have a component I am developing which I would like to have  
>>>>> create a direct link during render.  In order to do this, I need a  
>>>>> function that can return the appropriate listener at render.  What  
>>>>> I cannot determine is how to make a method which will return a  
>>>>> value appropriate for the listener binding of the DirectLink.
>>>>>
>>>>> Hopefully an example clarifies what I am attempting:
>>>>>
>>>>> in the component template...
>>>>> for each link
>>>>>  {
>>>>>     <a jwcid="@DirectLink" listener="link.listener"   
>>>>> parameters="ognl:link.parameters">
>>>>> }
>>>>>
>>>>> where the links will be changing, and thus possibly pointing to  
>>>>> different methods at run time.  How do I create the getListener()  
>>>>> method that would be needed in the above example to complete the  
>>>>> binding correctly.
>>>>>
>>>>> Hope I'm clear.  If not, I will certainly try to clarify.
>>>>>
>>>>> Spencer
>>>>>
>>>>>
>>>>> ------------------------------------------------------------------- 
>>>>> --
>>>>> To unsubscribe, e-mail:  
>>>>> tapestry-user-unsubscribe@jakarta.apache.org
>>>>> For additional commands, e-mail:  
>>>>> tapestry-user-help@jakarta.apache.org
>>>>>
>>>>>
>>>>
>>>> -------------------------------------------------------------------- 
>>>> -
>>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>>> For additional commands, e-mail:  
>>>> tapestry-user-help@jakarta.apache.org
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail:  
>>> tapestry-user-help@jakarta.apache.org
>>>
>>>
>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: How to return a method binding value?

Posted by Harish Krishnaswamy <hk...@comcast.net>.
Without understanding your real situation its kinda hard to nail it. 
Seems like the regular listener methods should work for you, something 
like the following:

<a jwcid="link" listener="ognl:listener" />

.java
public IActionListener getListener()
{
    // I hate these if .. else
    // if it is as simple as this, you could create a map that
    // maps your condition to the right listener
    if (xCondition)
        return getListeners().getListener("xListener");

    if (yCondition)
        return getListeners().getListener("yListener");
}

public void xListener(IRequestCycle cycle)
{
    ...
}

public void yListener(IRequestCycle cycle)
{
    ...
}

-Harish

SWC wrote:

> Alright, my understanding is expanding here, but I could still use 
> just a touch of clarification.
>
> Howard wrote:
> "The ognl:listener.foo mapping to method foo() makes use of Java 
> reflection to create the connection,
> but it is not required. Listeners objects don't have to be listener 
> methods, you can use inner
> classes, helper beans or what not ... as long as they implement the 
> interface."
>
> So I can bind to any object that defines the IActionListener 
> interface, but other than ognl, there is no way to pass in the method 
> as a parameter directly? There's no way to create an IActionListener 
> that wraps a method directly. Is that understanding correct?
>
> My options as I see them then, would be to do one of the following:
> 1) Create tiny classes, one for each method I had wanted to bind to. 
> Each would define a method which fulfilled the IActionListener 
> interface, and the component could bind each of the links using one of 
> those classes. I'm pretty limited in my knowledge of patterns, but I 
> think maybe there is one which is appropriate here where I would 
> construct a single object from which you could request the appropriate 
> "method class" as needed?
>
> 2) Always bind to the same method, but pre-pad the parameters with a 
> value that can be used to switch between a limited number of other 
> functions from within that function.
>
> The first is more powerful, but seems like too much work for what I am 
> trying to accomplish, and I don't know that the second will be 
> flexible enough for sure.
>
> Either way, thanks to both of you for your input.
>
> Spencer
>
>
> On Feb 11, 2004, at 11:34 PM, Harish Krishnaswamy wrote:
>
>> If you have a finite set of listeners that you want to juggle with, 
>> you can create one listener that invokes the appropriate listener and 
>> have all links bind to the one ubiquitous listener.
>>
>> -Harish
>>
>> Harish Krishnaswamy wrote:
>>
>>> AFAIK you can only achieve what you are trying to do with runtime 
>>> code generation that is assuming you want different behavior for 
>>> each link in the loop.
>>>
>>> -Harish
>>>
>>>
>>> SWC wrote:
>>>
>>>> I have a component I am developing which I would like to have 
>>>> create a direct link during render.  In order to do this, I need a 
>>>> function that can return the appropriate listener at render.  What 
>>>> I cannot determine is how to make a method which will return a 
>>>> value appropriate for the listener binding of the DirectLink.
>>>>
>>>> Hopefully an example clarifies what I am attempting:
>>>>
>>>> in the component template...
>>>> for each link
>>>>  {
>>>>     <a jwcid="@DirectLink" listener="link.listener"  
>>>> parameters="ognl:link.parameters">
>>>> }
>>>>
>>>> where the links will be changing, and thus possibly pointing to 
>>>> different methods at run time.  How do I create the getListener() 
>>>> method that would be needed in the above example to complete the 
>>>> binding correctly.
>>>>
>>>> Hope I'm clear.  If not, I will certainly try to clarify.
>>>>
>>>> Spencer
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>>
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: How to return a method binding value?

Posted by "Howard M. Lewis Ship" <hl...@comcast.net>.
Or, you could do what AbstractComponent and AbstractEngine do: define a listeners property:

private ListenerMap _listeners;

public ListenerMap getListeners()
{
  if (_listeners == null)
    _listeners = new ListenerMap(this);

  return _listeners;
}

Then, you can access listeners on your object as ognl:myObject.listeners.foo


--
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Tapestry: Java Web Components 
http://howardlewisship.com


> -----Original Message-----
> From: SWC [mailto:mail@crissman.net] 
> Sent: Thursday, February 12, 2004 8:12 PM
> To: Tapestry users
> Subject: Re: How to return a method binding value?
> 
> 
> Alright, my understanding is expanding here, but I could 
> still use just 
> a touch of clarification.
> 
> Howard wrote:
> "The ognl:listener.foo mapping to method foo() makes use of Java 
> reflection to create the connection,
> but it is not required. Listeners objects don't have to be listener 
> methods, you can use inner
> classes, helper beans or what not ... as long as they implement the 
> interface."
> 
> So I can bind to any object that defines the IActionListener 
> interface, 
> but other than ognl, there is no way to pass in the method as a 
> parameter directly? There's no way to create an IActionListener that 
> wraps a method directly. Is that understanding correct?
> 
> My options as I see them then, would be to do one of the following:
> 1) Create tiny classes, one for each method I had wanted to bind to. 
> Each would define a method which fulfilled the IActionListener 
> interface, and the component could bind each of the links 
> using one of 
> those classes. I'm pretty limited in my knowledge of patterns, but I 
> think maybe there is one which is appropriate here where I would 
> construct a single object from which you could request the 
> appropriate 
> "method class" as needed?
> 
> 2) Always bind to the same method, but pre-pad the parameters with a 
> value that can be used to switch between a limited number of other 
> functions from within that function.
> 
> The first is more powerful, but seems like too much work for 
> what I am 
> trying to accomplish, and I don't know that the second will 
> be flexible 
> enough for sure.
> 
> Either way, thanks to both of you for your input.
> 
> Spencer
> 
> 
> On Feb 11, 2004, at 11:34 PM, Harish Krishnaswamy wrote:
> 
> > If you have a finite set of listeners that you want to juggle with, 
> > you can create one listener that invokes the appropriate 
> listener and 
> > have all links bind to the one ubiquitous listener.
> >
> > -Harish
> >
> > Harish Krishnaswamy wrote:
> >
> >> AFAIK you can only achieve what you are trying to do with runtime 
> >> code generation that is assuming you want different 
> behavior for each 
> >> link in the loop.
> >>
> >> -Harish
> >>
> >>
> >> SWC wrote:
> >>
> >>> I have a component I am developing which I would like to 
> have create 
> >>> a direct link during render.  In order to do this, I need 
> a function 
> >>> that can return the appropriate listener at render.  What 
> I cannot 
> >>> determine is how to make a method which will return a value 
> >>> appropriate for the listener binding of the DirectLink.
> >>>
> >>> Hopefully an example clarifies what I am attempting:
> >>>
> >>> in the component template...
> >>> for each link
> >>>  {
> >>>     <a jwcid="@DirectLink" listener="link.listener"  
> >>> parameters="ognl:link.parameters">
> >>> }
> >>>
> >>> where the links will be changing, and thus possibly pointing to 
> >>> different methods at run time.  How do I create the getListener() 
> >>> method that would be needed in the above example to complete the 
> >>> binding correctly.
> >>>
> >>> Hope I'm clear.  If not, I will certainly try to clarify.
> >>>
> >>> Spencer
> >>>
> >>>
> >>> 
> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: 
> tapestry-user-unsubscribe@jakarta.apache.org
> >>> For additional commands, e-mail: 
> >>> tapestry-user-help@jakarta.apache.org
> >>>
> >>>
> >>
> >> 
> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: 
> tapestry-user-unsubscribe@jakarta.apache.org
> >> For additional commands, e-mail: 
> tapestry-user-help@jakarta.apache.org
> >>
> >>
> >
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: 
> tapestry-user-help@jakarta.apache.org
> >
> >
> >
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: How to return a method binding value?

Posted by SWC <ma...@crissman.net>.
Alright, my understanding is expanding here, but I could still use just 
a touch of clarification.

Howard wrote:
"The ognl:listener.foo mapping to method foo() makes use of Java 
reflection to create the connection,
but it is not required. Listeners objects don't have to be listener 
methods, you can use inner
classes, helper beans or what not ... as long as they implement the 
interface."

So I can bind to any object that defines the IActionListener interface, 
but other than ognl, there is no way to pass in the method as a 
parameter directly? There's no way to create an IActionListener that 
wraps a method directly. Is that understanding correct?

My options as I see them then, would be to do one of the following:
1) Create tiny classes, one for each method I had wanted to bind to. 
Each would define a method which fulfilled the IActionListener 
interface, and the component could bind each of the links using one of 
those classes. I'm pretty limited in my knowledge of patterns, but I 
think maybe there is one which is appropriate here where I would 
construct a single object from which you could request the appropriate 
"method class" as needed?

2) Always bind to the same method, but pre-pad the parameters with a 
value that can be used to switch between a limited number of other 
functions from within that function.

The first is more powerful, but seems like too much work for what I am 
trying to accomplish, and I don't know that the second will be flexible 
enough for sure.

Either way, thanks to both of you for your input.

Spencer


On Feb 11, 2004, at 11:34 PM, Harish Krishnaswamy wrote:

> If you have a finite set of listeners that you want to juggle with, 
> you can create one listener that invokes the appropriate listener and 
> have all links bind to the one ubiquitous listener.
>
> -Harish
>
> Harish Krishnaswamy wrote:
>
>> AFAIK you can only achieve what you are trying to do with runtime 
>> code generation that is assuming you want different behavior for each 
>> link in the loop.
>>
>> -Harish
>>
>>
>> SWC wrote:
>>
>>> I have a component I am developing which I would like to have create 
>>> a direct link during render.  In order to do this, I need a function 
>>> that can return the appropriate listener at render.  What I cannot 
>>> determine is how to make a method which will return a value 
>>> appropriate for the listener binding of the DirectLink.
>>>
>>> Hopefully an example clarifies what I am attempting:
>>>
>>> in the component template...
>>> for each link
>>>  {
>>>     <a jwcid="@DirectLink" listener="link.listener"  
>>> parameters="ognl:link.parameters">
>>> }
>>>
>>> where the links will be changing, and thus possibly pointing to 
>>> different methods at run time.  How do I create the getListener() 
>>> method that would be needed in the above example to complete the 
>>> binding correctly.
>>>
>>> Hope I'm clear.  If not, I will certainly try to clarify.
>>>
>>> Spencer
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: 
>>> tapestry-user-help@jakarta.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>

Re: How to return a method binding value?

Posted by Harish Krishnaswamy <hk...@comcast.net>.
If you have a finite set of listeners that you want to juggle with, you 
can create one listener that invokes the appropriate listener and have 
all links bind to the one ubiquitous listener.

-Harish

Harish Krishnaswamy wrote:

> AFAIK you can only achieve what you are trying to do with runtime code 
> generation that is assuming you want different behavior for each link 
> in the loop.
>
> -Harish
>
>
> SWC wrote:
>
>> I have a component I am developing which I would like to have create 
>> a direct link during render.  In order to do this, I need a function 
>> that can return the appropriate listener at render.  What I cannot 
>> determine is how to make a method which will return a value 
>> appropriate for the listener binding of the DirectLink.
>>
>> Hopefully an example clarifies what I am attempting:
>>
>> in the component template...
>> for each link
>>  {
>>     <a jwcid="@DirectLink" listener="link.listener"  
>> parameters="ognl:link.parameters">
>> }
>>
>> where the links will be changing, and thus possibly pointing to 
>> different methods at run time.  How do I create the getListener() 
>> method that would be needed in the above example to complete the 
>> binding correctly.
>>
>> Hope I'm clear.  If not, I will certainly try to clarify.
>>
>> Spencer
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: How to return a method binding value?

Posted by Harish Krishnaswamy <hk...@comcast.net>.
AFAIK you can only achieve what you are trying to do with runtime code 
generation that is assuming you want different behavior for each link in 
the loop.

-Harish


SWC wrote:

> I have a component I am developing which I would like to have create a 
> direct link during render.  In order to do this, I need a function 
> that can return the appropriate listener at render.  What I cannot 
> determine is how to make a method which will return a value 
> appropriate for the listener binding of the DirectLink.
>
> Hopefully an example clarifies what I am attempting:
>
> in the component template...
> for each link
>  {
>     <a jwcid="@DirectLink" listener="link.listener"  
> parameters="ognl:link.parameters">
> }
>
> where the links will be changing, and thus possibly pointing to 
> different methods at run time.  How do I create the getListener() 
> method that would be needed in the above example to complete the 
> binding correctly.
>
> Hope I'm clear.  If not, I will certainly try to clarify.
>
> Spencer
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: How to return a method binding value?

Posted by "Howard M. Lewis Ship" <hl...@comcast.net>.
Underneath the covers, the listener parameter is an instance of IActionListener.

The ognl:listener.foo mapping to method foo() makes use of Java reflection to create the connection,
but it is not required.  Listeners objects don't have to be listener methods, you can use inner
classes, helper beans or what not ... as long as they implement the interface.

An important note: you need to know the listener DURING THE NEW REQUEST not during the render.
That's when the listener parameter is used.

My approach?  I would create a new component that contained the DirectLink. It would have a listener
method that would, in turn, select the correct listener object to invoke (passed in as parameters,
perhaps). It would pass service parameters (bind the DirectLink's parameters parameter) through the
URL to help make that selection when the link is clicked (in the subsequent request).



--
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Tapestry: Java Web Components 
http://howardlewisship.com


> -----Original Message-----
> From: SWC [mailto:mail@crissman.net] 
> Sent: Wednesday, February 11, 2004 11:21 PM
> To: Tapestry users
> Subject: How to return a method binding value?
> 
> 
> I have a component I am developing which I would like to have 
> create a 
> direct link during render.  In order to do this, I need a 
> function that 
> can return the appropriate listener at render.  What I cannot 
> determine 
> is how to make a method which will return a value appropriate for the 
> listener binding of the DirectLink.
> 
> Hopefully an example clarifies what I am attempting:
> 
> in the component template...
> for each link
>   {
>      <a jwcid="@DirectLink" listener="link.listener"  
> parameters="ognl:link.parameters">
> }
> 
> where the links will be changing, and thus possibly pointing to 
> different methods at run time.  How do I create the getListener() 
> method that would be needed in the above example to complete the 
> binding correctly.
> 
> Hope I'm clear.  If not, I will certainly try to clarify.
> 
> Spencer
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org