aspose file tools*
The moose likes JSF and the fly likes JSF Listener discover the action method name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF Listener discover the action method name" Watch "JSF Listener discover the action method name" New topic
Author

JSF Listener discover the action method name

Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

Hi all.

I've been struggling to get the name of the method that started invoking my BackingBean.
I'm using a PhaseListener who is listening to invoke_app phase id.
Through this approach, I'd like to know the name of the method that fired this event.

I've been searching for the method name within the FacesContext , Application and ExternalContext but with no success.
I also tried to use reflection to get access to the "events" attribute of UIViewRoot (I saw inside the myfaces sourcecode). But... no success again...

Anybody know any way to achieve this? either by reflection or better using the API without any hacking.

The source code using reflection with no success



Thanks


Adolfo Eloy
Software Developer
OCPJP 6
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

First question: Why? What benefit will you have from knowing the method name?

Second question: Huh? The action method is fired directly from JSF. I should hope that it already knows who it is.


Customer surveys are for companies who didn't pay proper attention to begin with.
Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

Hello Tim Holloway!

Thanks for replying.
I need to do this because of a really specific scenario.
The project I'm working on use the JSF 1.1 spec and I need to invoke dynamically some methods from backing beans which implements some kind of interface.
But there is some times that a specific method cannot be executed dynamically and I'd like to detect when it was invoked.

I can't change this architecture, so I'd like to use some JSF hooks to achieve that.

But, I can get the "events" attribute now from the UIViewRoot.
I forgot to use the code below before retrieving the value of the attribute.



I know that I'm breaking this encapsulation but I don't have any idea to deal with this so specific scenario.

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

Well, it's hard for me to imagine what exactly it is you're dealing with, but I doubt that this is really a problem that needs JSF to solve it.

An Interface declaration is a contract that defines a specific set of methods that an implementing class will realize. So if you attach a bean - any bean, not just a JSF bean - to an invoking method (such as an action method) and that bean does implement that interface, then there will be some implementation of each of those interface methods. Maybe a dummy or stub, but at least something.

On the other hand, if the object in question doesn't define a contract (interface), then you have to go one step further and introspect the target object's method collection to see if it implements the method you need.

Introspection is part of the basic java.lang package set. You don't need JSF to introspect a bean, but JSF uses introspection internally to do things like locate and call property access methods and action methods.

Actually, JSF uses an extended Apache package called "beanutils". You can find it - and its documentation at the jakarta.apache.org website. It may be in one of the commons library collections these days. BeanUtils isn't essential for introspection, but it adds additional introspective capabilities that you might find useful.

I've done this kind of stuff many times. It's not really all that hard.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSF Listener discover the action method name