You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Bobby Rosenberger <br...@gmail.com> on 2005/11/17 21:39:06 UTC

Dynamic Link creation with ActionListener

Hey everyone,

I've floundered enough.

I would like to dynamically create a link that has an associated
actionListener. I understand how to do it using the tags... but dynamically,
I'm getting stumped... could someone help me over the hump? (my hump, my
hump, ... ok shoot me.) I'm creating the link as follows:

UICommand link = new UICommand();
link.setRendererType("javax.faces.Link");
link.setId("myLink");

I've also defined the following method:

public void listen(ActionEvent e){
String id = e.getComponent().getClientId(getContext());
System.out.println("Link Id: " + id);
}

Now the question is.. how the heck to I hood the two together? I would like
the "listen" method to be called when anyone clicks on the link...

Any help would be greatly appreciated.

Bobby

Re: Dynamic Link creation with ActionListener

Posted by Bobby Rosenberger <br...@gmail.com>.
Thanks guys,

I was a little brain-dead yesterday... this helped. I have it all working
now.

Thanks again,

Bobby Rosenberger


On 11/17/05, Craig McClanahan <cr...@apache.org> wrote:
>
> On 11/17/05, Bobby Rosenberger <br...@gmail.com> wrote:
>
> > Hey everyone,
> >
> > I've floundered enough.
> >
> > I would like to dynamically create a link that has an associated
> > actionListener. I understand how to do it using the tags... but dynamically,
> > I'm getting stumped... could someone help me over the hump? (my hump, my
> > hump, ... ok shoot me.) I'm creating the link as follows:
> >
> > UICommand link = new UICommand();
> > link.setRendererType("javax.faces.Link");
> > link.setId("myLink");
> >
> > I've also defined the following method:
> >
> > public void listen(ActionEvent e){
> > String id = e.getComponent().getClientId(getContext());
> > System.out.println("Link Id: " + id);
> > }
> >
> > Now the question is.. how the heck to I hood the two together? I would
> > like the "listen" method to be called when anyone clicks on the link...
>
>
> You might find it easier to use an action method, rather than an
> ActionListener, instead ... that way, it's easy to drive navigation. The
> steps involved are fairly simple:
>
> * Change your method signature to return a String and take no arguments
> (i.e. just like a statically linked action method)
>
> * Make sure this method is defined in a managed bean
> (for purposes of example, we'll assume it is under the
> name "foo"
>
> * Add the following logic to establish the linkage, via a
> method binding (note that this is almost exactly what
> the JSP tag does for <h:commandLink>:
>
> FacesContext fc = FacesContext.getCurrentInstance();
> MethodBinding mb = fc.getApplication().createMethodBinding("#{foo.listen}",
> null);
> link.setAction(mb);
>
> Any help would be greatly appreciated.
> >
> > Bobby
> >
> >
> Craig
>
>

Re: Dynamic Link creation with ActionListener

Posted by Craig McClanahan <cr...@apache.org>.
On 11/17/05, Bobby Rosenberger <br...@gmail.com> wrote:
>
> Hey everyone,
>
> I've floundered enough.
>
> I would like to dynamically create a link that has an associated
> actionListener. I understand how to do it using the tags... but dynamically,
> I'm getting stumped... could someone help me over the hump? (my hump, my
> hump, ... ok shoot me.) I'm creating the link as follows:
>
> UICommand link = new UICommand();
> link.setRendererType("javax.faces.Link");
> link.setId("myLink");
>
> I've also defined the following method:
>
> public void listen(ActionEvent e){
> String id = e.getComponent().getClientId(getContext());
> System.out.println("Link Id: " + id);
> }
>
> Now the question is.. how the heck to I hood the two together? I would
> like the "listen" method to be called when anyone clicks on the link...


You might find it easier to use an action method, rather than an
ActionListener, instead ... that way, it's easy to drive navigation. The
steps involved are fairly simple:

* Change your method signature to return a String and take no arguments
(i.e. just like a statically linked action method)

* Make sure this method is defined in a managed bean
(for purposes of example, we'll assume it is under the
name "foo"

* Add the following logic to establish the linkage, via a
method binding (note that this is almost exactly what
the JSP tag does for <h:commandLink>:

FacesContext fc = FacesContext.getCurrentInstance();
MethodBinding mb = fc.getApplication().createMethodBinding("#{foo.listen}",
null);
link.setAction(mb);

Any help would be greatly appreciated.
>
> Bobby
>
>
Craig

Re: Dynamic Link creation with ActionListener

Posted by Mike Kienenberger <mk...@gmail.com>.
        updateUICommand = (UICommand)
app.createComponent(UICommand.COMPONENT_TYPE);
        updateUICommand.setValue("Update");
        updateUICommand.setId("updateUICommand");

        updateUICommand.setActionListener(new YourListenMethodBinding());

You'll need to create your own MethodBinding subclass.   You can
probably have MethodBinding.invoke() directly call your listen method.
 Don't forget to implement StateHolder.

-Mike


On 11/17/05, Simon Kitching <sk...@obsidium.com> wrote:
> Bobby Rosenberger wrote:
> > Hey everyone,
> >
> > I've floundered enough.
> >
> > I would like to dynamically create a link that has an associated
> > actionListener. I understand how to do it using the tags... but
> > dynamically, I'm getting stumped... could someone help me over the hump?
> > (my hump, my hump, ... ok shoot me.) I'm creating the link as follows:
> >
> > UICommand link = new UICommand();
> > link.setRendererType("javax.faces.Link");
> > link.setId("myLink");
> >
> > I've also defined the following method:
> >
> > public void listen(ActionEvent e){
> >         String id = e.getComponent().getClientId(getContext());
> >         System.out.println("Link Id: " + id);
> >     }
> >
> > Now the question is.. how the heck to I hood the two together? I would
> > like the "listen" method to be called when anyone clicks on the link...
> >
>
> I've not done this programmatically myself, but I believe that this all
> follows the generic listener pattern.
>
> The class containing the listen method needs to implement interface
> ActionListener - or you need to create a simple class that does
> implement that interface (eg as an inner class or an anonymous class),
> just like the standard listener pattern for Swing or other apps.
>
> You then call
>    link.addFacesListener(yourListenerObject)
>
> Regards,
>
> Simon
>

Re: Dynamic Link creation with ActionListener

Posted by Simon Kitching <sk...@obsidium.com>.
Bobby Rosenberger wrote:
> Hey everyone,
> 
> I've floundered enough.
> 
> I would like to dynamically create a link that has an associated 
> actionListener. I understand how to do it using the tags... but 
> dynamically, I'm getting stumped... could someone help me over the hump? 
> (my hump, my hump, ... ok shoot me.) I'm creating the link as follows:
> 
> UICommand link = new UICommand();
> link.setRendererType("javax.faces.Link");
> link.setId("myLink");
> 
> I've also defined the following method:
> 
> public void listen(ActionEvent e){
>         String id = e.getComponent().getClientId(getContext());
>         System.out.println("Link Id: " + id);
>     }
> 
> Now the question is.. how the heck to I hood the two together? I would 
> like the "listen" method to be called when anyone clicks on the link...
> 

I've not done this programmatically myself, but I believe that this all 
follows the generic listener pattern.

The class containing the listen method needs to implement interface 
ActionListener - or you need to create a simple class that does 
implement that interface (eg as an inner class or an anonymous class), 
just like the standard listener pattern for Swing or other apps.

You then call
   link.addFacesListener(yourListenerObject)

Regards,

Simon